Find Kth element in an array containing odd elements first and then even elements | 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
function getNumber ( n , k ) { let pos ; if ( ( n % 2 ) == 0 ) { pos = parseInt ( n / 2 , 10 ) ; } else { pos = parseInt ( n / 2 , 10 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } let n = 8 , k = 5 ; document . write ( getNumber ( n , k ) ) ;