Find Kth element in an array containing odd elements first and then even elements | Java implementation of the approach ; Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code
import java . io . * ; class GFG { static int getNumber ( int n , int k ) { int pos ; if ( ( n % 2 ) == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } public static void main ( String [ ] args ) { int n = 8 , k = 5 ; System . out . println ( getNumber ( n , k ) ) ; } }