Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1' ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code
function rotateString ( n , m , s ) { var v = [ ] ; var cnt = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { v . push ( i ) ; } } for ( i = 1 ; i < v . length ; i ++ ) { if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) { cnt ++ ; } } if ( v . length >= 2 && ( n - ( v [ v . length - 1 ] - v [ 0 ] ) - 1 ) > m ) { cnt ++ ; } if ( cnt <= 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; var M = 1 ; var N = S . length ; rotateString ( N , M , S ) ;