Minimum time required to cover a Binary Array
Given an integer N which represents the size of a boolean array that is initially filled with 0, and also given an array arr[] of size K consisting of (1-based) indices at which ‘1’ are present in the boolean array. Now, at one unit of time the adjacent cells of the arr[i] in the boolean array become 1 that is 010 becomes 111. Find the minimum time required to convert the whole array into 1s.
Examples:
Input : N = 5, arr[] = {1, 4}
Output : 1
Explanation:
Initially the boolean array is of size 5 filled with 5 zeros. arr[] represents places at which 1 is present in the boolean array. Therefore, the boolean array becomes 10010.
Now, at time (t) = 1, the 0s at 3rd and 5th position becomes 1 => 10111 and at the same time, 0 at 2nd position becomes 1 => 11111. All the 1s initially increment their adjacent 0s at the same moment of time. So at t=1, the string gets converted to all 1s.
Input : N=7, arr[] = {1, 7}
Output : 3
Explanation:
At time (t) = 1, 1000001 becomes 1100011
At time (t) = 2, 1100011 becomes 1110111
At time (t) = 3, 1110111 becomes 1111111
Hence, minimum time is 3 to change the binary array into 1.
Approach:
To solve the problem mentioned above we have to observe that we need to find the longest segment of zeroes until 1 appears. For example, for a binary number 00010000010000, the longest segment of 0s is from 4th to 10th position. Now, observe that there are 5 0s between the indices which is an odd number. Hence, we can conclude that to cover 5 zeros we need 5/2 + 1 that is 3 units of time because all the other segments will get filled in less than or equal to 3 units of time.
- If the longest zero segments are odd then we can conclude that x/2 + 1 unit of time is required where x is the number of 0s in the longest segment.
- If the longest zero segments are even then we can conclude that x/2 units of time are required where x is the number of 0s in the longest segment.
We can calculate the maximum length contiguous segment until 1 occurs in the boolean array and return the answer depends upon whether the length is odd or even.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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);
}
|
Java
class GFG {
static int solve( int []arr, int n)
{
int k = arr.length;
int mp[] = new int [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 = Math.max(leftSegment, arr[i] - arr[i - 1 ] - 1 );
}
int rightSegment = n - arr[k - 1 ];
int maxSegment = Math.max(leftSegment, rightSegment);
int tim;
if ((maxSegment & 1 ) == 1 )
tim = (maxSegment / 2 ) + 1 ;
else
tim = maxSegment / 2 ;
return tim;
}
public static void main (String[] args)
{
int N = 5 ;
int arr[] = { 1 , 4 };
System.out.println(solve(arr, N));
}
}
|
Python3
def solve(arr, n) :
k = len (arr)
mp = [ False for i in range (n + 2 )]
for i in range (k) :
mp[arr[i]] = True
leftSegment = arr[ 0 ] - 1
for i in range ( 1 ,k) :
leftSegment = max (leftSegment, arr[i] - arr[i - 1 ] - 1 )
rightSegment = n - arr[k - 1 ]
maxSegment = max (leftSegment, rightSegment);
tim = 0
if (maxSegment & 1 ) :
tim = (maxSegment / / 2 ) + 1
else :
tim = maxSegment / / 2
return tim
N = 5
arr = [ 1 , 4 ]
print (solve(arr, N))
|
C#
using System;
class GFG{
static int solve( int [] arr, int n)
{
int k = arr.Length;
int [] mp = new int [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 = Math.Max(leftSegment,
arr[i] -
arr[i - 1] - 1);
}
int rightSegment = n - arr[k - 1];
int maxSegment = Math.Max(leftSegment,
rightSegment);
int tim;
if ((maxSegment & 1) == 1)
tim = (maxSegment / 2) + 1;
else
tim = maxSegment / 2;
return tim;
}
public static void Main ()
{
int N = 5;
int [] arr = { 1, 4 };
Console.Write(solve(arr, N));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...