Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | Java 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findKthElement ( int N , int K ) { ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . add ( i ) ; } } for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . add ( i ) ; } } System . out . println ( v . get ( K - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; } }