Kth largest odd number in a given range
Last Updated :
10 Feb, 2023
Given two variables L and R, indicating a range of integers from L to R inclusive, and a number K, the task is to find Kth largest odd number. If K > number of odd numbers in the range L to R then return 0.
Examples:
Input: L = -10, R = 10, K = 8
Output: -5
Explanation: The odd Numbers in the range are -9, -7, -5, -3, -1, 1, 3, 5, 7, 9 and the 8th Largest odd number is -5
Input: L = -3, R = 3, K = 1
Output: 3
Approach: The given problem can be solved using mathematics. The idea is to check if R is odd or even and calculate Kth largest odd number accordingly. Below steps can be used to solve the problem:
- If K<=0 then return 0
- Initialize count to calculate the number of odd numbers within the range
- If R is odd:
- count = ceil((float)(R-L+1)/2)
- If K > count return 0
- Else return (R - 2*K + 2)
- If R is even
- count = floor((R-L+1)/2)
- If K > count return 0
- Else return (R - 2*K + 1)
Below is the implementation of the above approach:
C++
// C++ implementation for the above approach
#include <cmath>
#include <iostream>
using namespace std;
// Function to return Kth largest
// odd number if it exists
int kthOdd(pair<int, int> range, int K)
{
// Base Case
if (K <= 0)
return 0;
int L = range.first;
int R = range.second;
if (R & 1) {
// Calculate count of odd
// numbers within the range
int Count = ceil((float)(R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
}
else {
// Calculate count of odd
// numbers within the range
int Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
int main()
{
// Initialize the range
pair<int, int> p = { -10, 10 };
// Initialize k
int k = 8;
// print the kth odd number
cout << kthOdd(p, k);
return 0;
}
Java
// Java implementation for the above approach
class GFG {
// Function to return Kth largest
// odd number if it exists
public static int kthOdd(int[] range, int K) {
// Base Case
if (K <= 0)
return 0;
int L = range[0];
int R = range[1];
if ((R & 1) > 0) {
// Calculate count of odd
// numbers within the range
int Count = (int) Math.ceil((R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
} else {
// Calculate count of odd
// numbers within the range
int Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
public static void main(String args[])
{
// Initialize the range
int[] p = { -10, 10 };
// Initialize k
int k = 8;
// print the kth odd number
System.out.println(kthOdd(p, k));
}
}
// This code is contributed by gfgking.
Python3
# python implementation for the above approach
import math
# Function to return Kth largest
# odd number if it exists
def kthOdd(range, K):
# Base Case
if (K <= 0):
return 0
L = range[0]
R = range[1]
if (R & 1):
# Calculate count of odd
# numbers within the range
Count = math.ceil((R - L + 1) / 2)
# if k > range then kth largest
# odd number is not in the range
if (K > Count):
return 0
else:
return (R - 2 * K + 2)
else:
# Calculate count of odd
# numbers within the range
Count = (R - L + 1) // 2
# If k > range then kth largest
# odd number is not in this range
if (K > Count):
return 0
else:
return (R - 2 * K + 1)
# Driver Code
if __name__ == "__main__":
# Initialize the range
p = [-10, 10]
# Initialize k
k = 8
# print the kth odd number
print(kthOdd(p, k))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
public class GFG
{
// Function to return Kth largest
// odd number if it exists
public static int kthOdd(int[] range, int K) {
// Base Case
if (K <= 0)
return 0;
int L = range[0];
int R = range[1];
if ((R & 1) > 0) {
// Calculate count of odd
// numbers within the range
int Count = ((R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
} else {
// Calculate count of odd
// numbers within the range
int Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
public static void Main(string[] args)
{
// Initialize the range
int[] p = { -10, 10 };
// Initialize k
int k = 8;
// print the kth odd number
Console.WriteLine(kthOdd(p, k));
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to return Kth largest
// odd number if it exists
function kthOdd(range, K) {
// Base Case
if (K <= 0)
return 0;
let L = range.first;
let R = range.second;
if (R & 1) {
// Calculate count of odd
// numbers within the range
let Count = Math.ceil((R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
}
else {
// Calculate count of odd
// numbers within the range
let Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
// Initialize the range
let p = { first: -10, second: 10 };
// Initialize k
let k = 8;
// print the kth odd number
document.write(kthOdd(p, k));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach: using NumPy:
Python3
import numpy as np
def kth_largest_odd_number(start, end, k):
# create an array of odd numbers within the range
odd_numbers = np.array([x for x in range(start, end+1) if x % 2 != 0])
# sort the array in descending order
sorted_odd_numbers = np.sort(odd_numbers)[::-1]
# return the kth largest odd number
return sorted_odd_numbers[k-1]
if __name__ == '__main__':
start = -10
end = 10
k = 8
result = kth_largest_odd_number(start, end, k)
print(result)
output
-5
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find the largest odd number in String Given a string S, representing a large integer, the task is to return the largest-valued odd integer (as a string) that is a substring of the given string S. Note: A substring is a contiguous sequence of characters within a string. A null string ("") is also a substring. Examples: Input: S = "504"Ou
4 min read
Largest Even and Odd N-digit numbers Given an integer N, the task is to find the largest even and odd N-digit numbers.Examples: Input: N = 4 Output: Even = 9998 Odd = 9999Input: N = 2 Output: Even = 98 Odd = 99 Approach: Largest N-digit even number will be (10n) - 2 because the series for different values of N will be 8, 98, 998, 9998,
3 min read
Kth smallest even number in range L to R Given two variables L and R, indicating a range of integers from L to R inclusive, and a number K, the task is to find Kth smallest even number. If K is greater than a number of even numbers in the range L to R then return -1. LLONG_MIN <= L <= R <= LLONG_MAX. Examples: Input: L = 3, R = 9,
6 min read
Kth largest even number in N intervals Given a 2D array arr[] of N ranges, indicating integer intervals from L to R inclusive, and a number K, the task is to find Kth largest even number. Examples: Input: arr = {{12, 15}, {3, 9}, {2, 5}, {20, 25}, {16, 20}}, K = 9Output: 6Explanation: After merging, the ranges become {{2, 9}, {12, 25}} t
11 min read
Largest Even and Odd N-digit numbers in Octal Number System Given an integer N, the task is to find the largest even and odd N-digit numbers in Octal Number System.Examples: Input: N = 4 Output: Even : 7776 Odd : 7777Input: N = 2 Output: Even : 76 Odd : 77 Approach: To get the largest number, the digits of the number have to be maximum possible. Since in the
4 min read
Greatest odd factor of an even number Given an even number N, the task is to find the greatest possible odd factor of N. Examples: Input: N = 8642 Output: 4321 Explanation: Here, factors of 8642 are {1, 8642, 2, 4321, 29, 298, 58, 149} in which odd factors are {1, 4321, 29, 149} and the greatest odd factor among all odd factors is 4321.
4 min read