Given two integers n and k, the task is to find the kth smallest element from the range [1, n] after deleting all the odd numbers from the range.
Examples:
Input: n = 8, k = 3
Output: 6
After deleting all the odd numbers from the range [1, 8]
2, 4, 6 and 8 are the only numbers left and 6 is the 3rd smallest.
Input: n = 8, k = 4
Output:
Approach: Since all odd numbers are removed so now only even numbers are left i.e. 2, 4, 6, 8, …..
Now, the kth smallest element will always be 2 * k.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int kthSmallest( int n, int k)
{
return (2 * k);
}
int main()
{
int n = 8, k = 4;
cout << kthSmallest(n, k);
return 0;
}
|
Java
class GFG {
static int kthSmallest( int n, int k)
{
return ( 2 * k);
}
public static void main(String args[])
{
int n = 8 , k = 4 ;
System.out.print(kthSmallest(n, k));
}
}
|
Python3
def kthSmallest(n, k):
return 2 * k
n = 8 ; k = 4
print (kthSmallest(n, k))
|
C#
using System;
class GFG {
static int kthSmallest( int n, int k)
{
return (2 * k);
}
public static void Main()
{
int n = 8, k = 4;
Console.WriteLine(kthSmallest(n, k));
}
}
|
PHP
<?php
function kthSmallest( $n , $k )
{
return (2 * $k );
}
$n = 8; $k = 4;
echo (kthSmallest( $n , $k ));
?>
|
Javascript
<script>
function kthSmallest(n, k)
{
return (2 * k);
}
var n = 8, k = 4;
document.write(kthSmallest(n, k));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)