Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | 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
function findKthElement ( N , K ) { var v = [ ] ; for ( i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . push ( i ) ; } } for ( i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . push ( i ) ; } } document . write ( v [ K - 1 ] ) ; } var N = 10 , K = 3 ; findKthElement ( N , K ) ;