Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order.
Examples :
Input: N = 10, K = 3
Output: 6
Explanation:
The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.
The 3rd number in the permutation is 6.Input: N = 5, K = 4
Output: 3
Explanation:
The required permutation is {2, 4, 1, 3, 5}.
The 4th number in the permutation is 3.
Naive Approach: The simplest approach to solve the problem is to generate the required permutation of first N natural numbers and then traverse the permutation to find the Kth element present in it.
Follow the steps below to solve the problem:
- Initialize an array, say V[] of size N., to store the required sequence.
- Insert all even numbers less than or equal to N into V[].
- Then, insert all odd numbers less than or equal to N into V[].
- After forming the array, print the value of V[K – 1] as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the K-th element // in the required permutation void findKthElement( int N, int K)
{ // Stores the required permutation
vector< int > v;
// Insert all the even numbers
// less than or equal to N
for ( int i = 1; i <= N; i++) {
if (i % 2 == 0) {
v.push_back(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for ( int i = 1; i <= N; i++) {
if (i % 2 != 0) {
v.push_back(i);
}
}
// Print the Kth element
cout << v[K - 1];
} // Driver Code int main()
{ int N = 10, K = 3;
findKthElement(N, K);
return 0;
} |
// Java program for the above approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{ // Function to find the K-th element
// in the required permutation
static void findKthElement( int N, int K)
{
// Stores the required permutation
ArrayList<Integer> v = new ArrayList<>();
// Insert all the even numbers
// less than or equal to N
for ( int i = 1 ; i <= N; i++) {
if (i % 2 == 0 ) {
v.add(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for ( int i = 1 ; i <= N; i++) {
if (i % 2 != 0 ) {
v.add(i);
}
}
// Print the Kth element
System.out.println(v.get(K - 1 ));
}
// Driver code
public static void main(String[] args)
{
int N = 10 , K = 3 ;
// functions call
findKthElement(N, K);
}
} // This code is contributed by Kingash. |
# python 3 program for the above approach # Function to find the K-th element # in the required permutation def findKthElement(N, K):
# Stores the required permutation
v = []
# Insert all the even numbers
# less than or equal to N
for i in range ( 1 , N + 1 ):
if (i % 2 = = 0 ):
v.append(i)
# Now, insert all odd numbers
# less than or equal to N
for i in range ( 1 , N + 1 ):
if (i % 2 ! = 0 ):
v.append(i)
# Print the Kth element
print (v[K - 1 ])
# Driver Code if __name__ = = "__main__" :
N = 10
K = 3
findKthElement(N, K)
# This code is contributed by ukasp.
|
// C# program for above approach using System;
using System.Collections.Generic;
public class GFG
{ // Function to find the K-th element
// in the required permutation
static void findKthElement( int N, int K)
{
// Stores the required permutation
List< int > v = new List< int >();
// Insert all the even numbers
// less than or equal to N
for ( int i = 1; i <= N; i++) {
if (i % 2 == 0) {
v.Add(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for ( int i = 1; i <= N; i++) {
if (i % 2 != 0) {
v.Add(i);
}
}
// Print the Kth element
Console.WriteLine(v[K - 1]);
}
// Driver code
public static void Main(String[] args)
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
} // This code is contributed by susmitakundugoaldanga. |
<script> // JavaScript program for the above approach // Function to find the K-th element
// in the required permutation
function findKthElement(N , K) {
// Stores the required permutation
var v = [];
// Insert all the even numbers
// less than or equal to N
for (i = 1; i <= N; i++) {
if (i % 2 == 0) {
v.push(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for (i = 1; i <= N; i++) {
if (i % 2 != 0) {
v.push(i);
}
}
// Print the Kth element
document.write(v[K - 1]);
}
// Driver code
var N = 10, K = 3;
// functions call
findKthElement(N, K);
// This code contributed by Rajput-Ji </script> |
6
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the observation that the first N / 2 elements are even and the value of the Kthelement in the first half is equal to K * 2. If K > N/2, the value of the Kthelement, depends on whether N is odd or even.
Follow the steps below to solve the problem:
- Initialize a variable, say ans, to store the Kthelement.
- Check if the value of K ≤ N/2. If found to be true, update ans to K*2.
- Otherwise, K lies in the second half. In this case, ans depends on the value of N.
- If the value of N is even, update ans to (K*2)-N-1.
- Otherwise, update ans to (K*2)-N.
- Print the value of ans as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the Kth element // in the required permutation void findKthElement( int N, int K)
{ // Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
cout << ans;
} // Driver Code int main()
{ int N = 10, K = 3;
findKthElement(N, K);
return 0;
} |
// Java program for the above approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the Kth element
// in the required permutation
static void findKthElement( int N, int K)
{
// Store the required result
int ans = 0 ;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2 ) {
ans = K * 2 ;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0 ) {
ans = (K * 2 ) - N - 1 ;
}
// If N is odd
else {
ans = (K * 2 ) - N;
}
}
// Print the required result
System.out.println(ans);
}
// Driver code
public static void main(String[] args)
{
int N = 10 , K = 3 ;
// functions call
findKthElement(N, K);
}
} // This code is contributed by Kingash. |
# Python 3 program for the above approach # Function to find the Kth element # in the required permutation def findKthElement(N, K):
# Store the required result
ans = 0
# If K is in the first
# N / 2 elements, print K * 2
if (K < = N / 2 ):
ans = K * 2
# Otherwise, K is greater than N/2
else :
# If N is even
if (N % 2 = = 0 ):
ans = (K * 2 ) - N - 1
# If N is odd
else :
ans = (K * 2 ) - N
# Print the required result
print (ans)
# Driver Code if __name__ = = '__main__' :
N = 10
K = 3
findKthElement(N, K)
# This code is contributed by ipg2016107.
|
// C# program for the above approach using System;
class GFG{
// Function to find the Kth element
// in the required permutation
static void findKthElement( int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
Console.Write(ans);
}
// Driver code
static void Main()
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
} // This code is contributed by sanjoy_62. |
<script> // javascript program for the above approach // Function to find the Kth element // in the required permutation function findKthElement( N, K)
{ // Store the required result
let ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2)
{
ans = K * 2;
}
// Otherwise, K is greater than N/2
else
{
// If N is even
if (N % 2 == 0)
{
ans = (K * 2) - N - 1;
}
// If N is odd
else
{
ans = (K * 2) - N;
}
}
// Print the required result
document.write(ans);
} // Driver Code let N = 10, K = 3;
findKthElement(N, K);
// This code is contributed by todaysgaurav </script> |
6
Time Complexity: O(1)
Auxiliary Space: O(1)