Kth smallest even number in range L to R
Last Updated :
23 Feb, 2022
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, K = 3
Output: 8
Explanation: The even numbers in the range are 4, 6, 8 and the 3rd smallest even number is 8
Input: L = -3, R = 3, K = 2
Output: 0
Naive Approach: The basic idea is to traverse the numbers from L to R, and then print the Kth even number.
Time Complexity: O(R-L)
Auxiliary Space: O(1)
Approach: The given problem can be solved using basic maths and using ceil and floor from cmath library. The idea is to check if L is odd or even and calculate Kth smallest even number accordingly. Below steps can be used to solve the problem:
- If K<=0 then return -1
- Initialize count to calculate the number of even numbers within the range
- If L is odd
- count = floor((float)(R-L+1)/2)
- If K > count return -1
- Else return (L + 2*K – 1)
- If L is even
- count = ceil((float)(R-L+1)/2)
- If K > count return -1
- Else return (L + 2*K – 2)
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
#define ll long long
using namespace std;
ll findEven(ll L, ll R, ll K)
{
if (K <= 0)
return -1;
if (L % 2) {
ll Count = floor (( float )(R - L + 1) / 2);
return (K > Count) ? -1 : (L + 2 * K - 1);
}
else {
ll Count = ceil (( float )(R - L + 1) / 2);
return (K > Count) ? -1 : (L + 2 * K - 2);
}
}
int main()
{
ll L = 3, R = 9, K = 3;
cout << findEven(L, R, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static long findEven( long L, long R, long K)
{
if (K <= 0 )
return - 1 ;
if (L % 2 == 1 ) {
long Count = ( int )Math.floor(( float )(R - L + 1 ) / 2 );
return (K > Count) ? - 1 : (L + 2 * K - 1 );
}
else {
long Count = ( int )Math.ceil(( float )(R - L + 1 ) / 2 );
return (K > Count) ? - 1 : (L + 2 * K - 2 );
}
}
public static void main(String args[])
{
long L = 3 , R = 9 , K = 3 ;
System.out.println(findEven(L, R, K));
}
}
|
Python3
def findEven(L, R, K):
if (K < = 0 ):
return - 1
if (L % 2 ):
Count = (R - L + 1 ) / / 2
return - 1 if (K > Count) else (L + 2 * K - 1 )
else :
Count = (R - L + 1 ) / / 2
return - 1 if (K > Count) else (L + 2 * K - 2 )
L = 3
R = 9
K = 3
print (findEven(L, R, K))
|
C#
using System;
class GFG
{
static long findEven( long L, long R, long K)
{
if (K <= 0)
return -1;
if (L % 2 == 1) {
long Count = ( int )Math.Floor(( float )(R - L + 1) / 2);
return (K > Count) ? -1 : (L + 2 * K - 1);
}
else {
long Count = ( int )Math.Ceiling(( float )(R - L + 1) / 2);
return (K > Count) ? -1 : (L + 2 * K - 2);
}
}
public static void Main()
{
long L = 3, R = 9, K = 3;
Console.Write(findEven(L, R, K));
}
}
|
Javascript
<script>
function findEven(L, R, K)
{
if (K <= 0)
return -1;
if (L % 2) {
let Count = Math.floor((R - L + 1) / 2);
return (K > Count) ? -1 : (L + 2 * K - 1);
}
else {
let Count = Math.ceil((float)(R - L + 1) / 2);
return (K > Count) ? -1 : (L + 2 * K - 2);
}
}
let L = 3, R = 9, K = 3;
document.write(findEven(L, R, K));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...