Kth character from the Nth string obtained by the given operations | String reverse ; Function to return Kth character from recursive String ; If N is 1 then return A ; Iterate a loop and generate the recursive String ; Update current String ; Change A to B and B to A ; Reverse the previous String ; Return the kth character ; Driver Code
function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } function findKthChar ( n , k ) { let prev = " " ; let cur = " " ; if ( n == 1 ) { return ' ' ; } for ( let j = 2 ; j <= n ; j ++ ) { cur = prev + " " ; for ( let i = 0 ; i < prev . length ; i ++ ) { if ( prev [ i ] == ' ' ) { prev [ prev [ i ] ] = ' ' ; } else { prev [ prev [ i ] ] = ' ' ; } } prev = reverse ( prev ) ; cur += prev ; prev = cur ; } return cur [ k ] ; } let N = 4 ; let K = 3 ; document . write ( findKthChar ( N , K ) ) ;