Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver Code
function ConstructBinaryString ( N , M , K ) { if ( M < ( N - 1 ) || M > K * ( N + 1 ) ) return " " ; let ans = " " ; let l = Math . min ( K , M / ( N - 1 ) ) ; let temp = N ; while ( temp != 0 ) { temp -- ; ans += ' ' ; if ( temp == 0 ) break ; for ( let i = 0 ; i < l ; i ++ ) { ans += ' ' ; } } M -= ( N - 1 ) * l ; if ( M == 0 ) return ans ; l = Math . min ( M , K ) ; for ( let i = 0 ; i < l ; i ++ ) ans += ' ' ; M -= l ; while ( M > 0 ) { ans = ' ' + ans ; M -- ; } return ans ; } let N = 5 , M = 9 , K = 2 ; document . write ( ConstructBinaryString ( N , M , K ) ) ;