Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | C ++ program for the above approach ; Function to find the Kth element in the required permutation ; Store the required result ; If K is in the first N / 2 elements , print K * 2 ; Otherwise , K is greater than N / 2 ; If N is even ; If N is odd ; Print the required result ; Driver Code ; functions call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findKthElement ( int N , int K ) { int ans = 0 ; if ( K <= N / 2 ) { ans = K * 2 ; } else { if ( N % 2 == 0 ) { ans = ( K * 2 ) - N - 1 ; } else { ans = ( K * 2 ) - N ; } } cout << ans ; } int main ( ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; return 0 ; }