Last Updated: 18 Nov, 2020

Maximum Consecutive Ones

Moderate
Asked in companies
FacebookMakeMyTripAmazon

Problem statement

Given a binary array 'ARR' of size 'N', your task is to find the longest sequence of continuous 1’s that can be formed by replacing at-most 'K' zeroes by ones. Return the length of this longest sequence of continuous 1’s.

Input format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.

The first line of each test case or query contains an integer 'N' representing the size of the array (ARR).

The second line contains 'N' single space-separated binary values, representing the elements in the array.

The third line contains the value of 'K'.
Output format:
For each test case, return the length of the longest subarray whose all elements are 1.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10^4 
0 <= Arr[i] <= 1
0 <= K <= N

Time Limit: 1 sec

Approaches

01 Approach

The simplest way to find the required subarray would be to consider all the possible subarrays and compare the length at every point and store the longest length. If at any point, the current element is 0 and the value of ‘K’ is greater than 0, use it to convert the current element to 1. Else if the value of K is 0 , iterate further to find the next possible subarray.

 

Algorithm : 

  1. Loop over the array and if the current element of the array is ‘1’ find the length of the longest subarray starting with the current element.
  2. If the current element is ‘0’ , and the value of ‘K’ is greater than 0 , use it to convert the current element to ‘1’ and continue.
  3. While considering any particular subarray if the value of any element is 0 and the value of K is greater than 0, reduce K by 1 and consider the current element to be 1.
  4. At every step update the length of the longest subarray of 1’s found so far, print this value at the end.

02 Approach

The idea is to iterate the whole array and push the indices having value as zero of the subarray considered in a queue. Keep on iterating the array until the size of the queue is less than ‘K’. When the size of the queue becomes equal to ‘K’, update the starting index of the subarray under consideration and pop an element from the queue and push the value of current index in the queue. Check at every step for maximum size.

 

Algorithm : 

  1. Loop over the array and keep a mark of the starting index of the subarray under consideration and take an empty queue.
  2. If the value at current index of array is 0, check the size of the queue:
    a. If the size of the queue is less than ‘K’, push the index.
    b. Else update the value of the starting index as the next index present at the front of the queue, pop the front value of the queue and push the current index at the back.
  3. At every step update the length of the longest subarray of 1’s found so far, print this value at the end.

03 Approach

The idea is to use Two - Pointer approach. Let us take a subarray [l,r] which contains at-most ‘K’ zeroes. Let our left pointer be ‘l’ and right pointer be ‘r’. We always maintain our subsegment [l,r] to contain no more than ‘K’ zeroes by moving the left pointer ‘l’ accordingly. Check at every step for maximum size i.e. ‘r-l+1’.

 

Algorithm :

  1. Take two pointers ‘l’ and ‘r’ representing left and right pointers respectively. Initialize both the pointers with 0.
  2. Keep on moving the right pointer ‘r’ until the count of 0’s in the subarray represented by [l,r] is less than equal to ‘K’ (maximum replacements possible).
  3. When the count of 0’s becomes more than ‘K’ shorten the subarray by incrementing the left pointer ‘l’ so that the count of 0’s in subarray [l,r] becomes equal to ‘K’.
  4. At every step update the length of the longest subarray found so far, print this value at the end.