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 K - th element in the required permutation ; Stores the required permutation ; Insert all the even numbers less than or equal to N ; Now , insert all odd numbers less than or equal to N ; Print the Kth element ; Driver Code ; functions call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findKthElement ( int N , int K ) { vector < int > v ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . push_back ( i ) ; } } for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . push_back ( i ) ; } } cout << v [ K - 1 ] ; } int main ( ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; return 0 ; }