Minimum time required to cover a Binary Array | CPP implementation to find the 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 ; driver code ; initialise N ; array initialisation
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( vector < int > arr , int n ) { int k = arr . size ( ) ; bool mp [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = max ( leftSegment , rightSegment ) ; int tim ; if ( maxSegment & 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } int main ( ) { int N = 5 ; vector < int > arr = { 1 , 4 } ; cout << solve ( arr , N ) ; }