Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s | Function to return the count ; Flipping zeros at distance " d " ; Function to implement binary search ; Check for valid distance i . e mid ; Driver code
function check ( arr , n , m , d ) { var i = 0 ; while ( i < n && m > 0 ) { m -- ; i += d ; } return m == 0 ? true : false ; } function maximumDistance ( arr , n , m ) { var low = 1 , high = n - 1 ; var ans = 0 ; while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var flag = check ( arr , n , m , mid ) ; if ( flag ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } var n = 5 , m = 3 ; var arr = new Array ( n ) ; arr . fill ( 0 ) ; document . write ( maximumDistance ( arr , n , m ) ) ;