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++
#include <cmath>
#include <iostream>
using namespace std;
int kthOdd(pair< int , int > range, int K)
{
if (K <= 0)
return 0;
int L = range.first;
int R = range.second;
if (R & 1) {
int Count = ceil (( float )(R - L + 1) / 2);
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
}
else {
int Count = (R - L + 1) / 2;
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
int main()
{
pair< int , int > p = { -10, 10 };
int k = 8;
cout << kthOdd(p, k);
return 0;
}
|
Java
class GFG {
public static int kthOdd( int [] range, int K) {
if (K <= 0 )
return 0 ;
int L = range[ 0 ];
int R = range[ 1 ];
if ((R & 1 ) > 0 ) {
int Count = ( int ) Math.ceil((R - L + 1 ) / 2 );
if (K > Count)
return 0 ;
else
return (R - 2 * K + 2 );
} else {
int Count = (R - L + 1 ) / 2 ;
if (K > Count)
return 0 ;
else
return (R - 2 * K + 1 );
}
}
public static void main(String args[])
{
int [] p = { - 10 , 10 };
int k = 8 ;
System.out.println(kthOdd(p, k));
}
}
|
Python3
import math
def kthOdd( range , K):
if (K < = 0 ):
return 0
L = range [ 0 ]
R = range [ 1 ]
if (R & 1 ):
Count = math.ceil((R - L + 1 ) / 2 )
if (K > Count):
return 0
else :
return (R - 2 * K + 2 )
else :
Count = (R - L + 1 ) / / 2
if (K > Count):
return 0
else :
return (R - 2 * K + 1 )
if __name__ = = "__main__" :
p = [ - 10 , 10 ]
k = 8
print (kthOdd(p, k))
|
C#
using System;
public class GFG
{
public static int kthOdd( int [] range, int K) {
if (K <= 0)
return 0;
int L = range[0];
int R = range[1];
if ((R & 1) > 0) {
int Count = ((R - L + 1) / 2);
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
} else {
int Count = (R - L + 1) / 2;
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
public static void Main( string [] args)
{
int [] p = { -10, 10 };
int k = 8;
Console.WriteLine(kthOdd(p, k));
}
}
|
Javascript
<script>
function kthOdd(range, K) {
if (K <= 0)
return 0;
let L = range.first;
let R = range.second;
if (R & 1) {
let Count = Math.ceil((R - L + 1) / 2);
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
}
else {
let Count = (R - L + 1) / 2;
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
let p = { first: -10, second: 10 };
let k = 8;
document.write(kthOdd(p, k));
</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):
odd_numbers = np.array([x for x in range (start, end + 1 ) if x % 2 ! = 0 ])
sorted_odd_numbers = np.sort(odd_numbers)[:: - 1 ]
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)
Share your thoughts in the comments
Please Login to comment...