Find iâ €™ th index character in a binary string obtained after n iterations | Set 2 | Function to find the i - th character ; distance between two consecutive elements after N iterations ; binary representation of M ; kth digit will be derived from root for sure ; Check whether there is need to flip root or not ; Driver Code
< ? php function KthCharacter ( $ m , $ n , $ k ) { $ distance = pow ( 2 , $ n ) ; $ Block_number = intval ( $ k / $ distance ) ; $ remaining = $ k % $ distance ; $ s = array ( 32 ) ; $ x = 0 ; for ( ; $ m > 0 ; $ x ++ ) { $ s [ $ x ] = $ m % 2 ; $ m = intval ( $ m / 2 ) ; } $ root = $ s [ $ x - 1 - $ Block_number ] ; if ( $ remaining == 0 ) { echo $ root . " STRNEWLINE " ; return ; } $ flip = true ; while ( $ remaining > 1 ) { if ( $ remaining & 1 ) { $ flip = ! $ flip ; } $ remaining = $ remaining >> 1 ; } if ( $ flip ) { echo ! $ root . " STRNEWLINE " ; } else { echo $ root . " STRNEWLINE " ; } } $ m = 5 ; $ k = 5 ; $ n = 3 ; KthCharacter ( $ m , $ n , $ k ) ; ? >