Minimum time required to cover a Binary Array | function to calculate the time ; Map to mark or store the binary values ; Firstly fill the boolean array with all zeroes ; Mark the 1 s ; Number of 0 s until first '1' occurs ; Maximum Number of 0 s in between 2 '1' s . ; Number of 0 s from right until first '1' occurs ; Return maximum from left and right segment ; check if count is odd ; check ifcount is even ; return the time ; initialise N ; array initialisation
function solve ( arr , n ) { let k = arr . length ; let mp = Array . from ( { length : n + 2 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( let i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } let leftSegment = arr [ 0 ] - 1 ; for ( let i = 1 ; i < k ; i ++ ) { leftSegment = Math . max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } let rightSegment = n - arr [ k - 1 ] ; let maxSegment = Math . max ( leftSegment , rightSegment ) ; let tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } let N = 5 ; let arr = [ 1 , 4 ] ; document . write ( solve ( arr , N ) ) ;