Value of k | JavaScript program to fin k - th element after append and insert middle operations ; Middle element of the sequence ; length of the resulting sequence . ; Updating the middle element of next sequence ; Moving to the left side of the middle element . ; Moving to the right side of the middle element . ; Driver code
function findElement ( n , k ) { let ans = n ; let left = 1 ; let right = ( Math . pow ( 2 , n ) - 1 ) ; while ( true ) { let mid = ( left + right ) / 2 ; if ( k == mid ) { document . write ( ans ) ; break ; } ans -- ; if ( k < mid ) { right = mid - 1 ; } else { left = mid + 1 ; } } } let n = 4 , k = 8 ; findElement ( n , k ) ;