Find Kth element in an array containing odd elements first and then even elements | Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
function getNumber ( n , k ) { var arr = Array ( n ) . fill ( n ) ; var i = 0 ; var odd = 1 ; while ( odd <= n ) { arr [ i ++ ] = odd ; odd += 2 ; } var even = 2 ; while ( even <= n ) { arr [ i ++ ] = even ; even += 2 ; } return arr [ k - 1 ] ; } var n = 8 , k = 5 ; document . write ( getNumber ( n , k ) ) ;