Longest subsegment of 1’s formed by changing at most k 0’s (Using Queue)


In this article, we will find the longest subsegment of 1’s which can be formed by changing at most k 0’s to 1’s. We will be using queue data structure to solve this problem.

In the approach discussed in this article, we will use a queue data structure to find the longest subarray containing only 1’s, which can be formed by changing at most k 0’s into 1’s. The queue data structure will be used to store the indices of 0 elements that have occurred previously. Whenever we encounter a new 0, we will check the size of the queue. If the size is less than k, then we can yet convert even more 0’s to 1’s. If the size is already k, we remove the first 0 that we added, figure out the current answer, and remove the first 0 from the queue. We then add the index of the current 0 to the queue. We repeat until we reach the end of the array. After that we calculate the answer once again for the edge case that the longest subsegment is formed at the end of the array.

Problem Statement

Given an array of size N, containing of 0’s and 1’s, we need to find the longest subarray or subsegment that can be formed if we are allowed to change at most k 0’s to 1’s.

Sample Examples

Input

arr = {1,0,1,0,1,0}, k=2

Output

5

Explanation

On of the possible solutions can to to change the 0 at indices 1 and 3 to 1’s to get the following array: {1,1,1,1,1,0}

Hence, the output is 5.

Input

arr = {1,0,0,1,0,1}, k=4

Output

6

Explanation

We can change all the 0’s to 1’s to get the array: {1,1,1,1,1,1}

So, the output will be 6.

Input

arr = {0,0,1,0,1,1,0,1}, k=1

Output

4

Explanation

We can change the 0 at index either 3 or 6 to get the array {0,0,1,1,1,1,0,1} or {0,0,1,0,1,1,1,1}

In both cases the answer will be 4.

Approach

In this approach we will use a method which involves utilizing a queue data structure to identify the longest subarray comprised solely of 1’s. This subarray can be obtained by converting a maximum of k 0’s into 1’s. The queue will be employed to retain indices of previously encountered 0 elements. Upon encountering a new 0, the queue's size is examined. If the size is below k, there is room to convert additional 0's. Once the size reaches k, the first 0 added is removed. At this point, the current outcome is evaluated, and the initial 0 is eliminated from the queue. Subsequently, the index of the present 0 is included in the queue. This process is repeated until the array's conclusion is reached. Following that, the result is recalculated to account for scenarios where the longest subsegment forms at the array's end.

Algorithm

  • Step 1 − Initialize a queue data structure.

  • Step 1.1 − Initialize three integer variables with 0, the current index, the lowest index we can take without converting more than k 0’s, the result.

  • Step 2 − Loop till the current index is less than the size of the array

  • Step 3 − In each iteration:

  • Step 3.1 − If the current element is 1, move on to the next element.

  • Step 3.2 − If the current element is 0 and the size of the queue is less than k, add the current index to the queue and move on to the next element.

  • Step 3.3 − If the current element is 1 and the size of the queue is equal to k, update the result as result = max (result, current index − lowest index), pop the first element from the queue and update the lowest index to 1+first element and finally push the current index to the queue.

  • Step 4 − After the loop ends, update the result once more for handling the case in which the longest subsegment is at the end of the array.

  • Step 5 − return the result.

Example

#include <bits/stdc++.h> 
using namespace std; 
int Longest_subsegment_of_ones(vector<int> &arr,int k) {
   queue<int> indices;
   int i=0, low=0;
   int res = 0;
   while(i<arr.size()){
      if(arr[i]==0){
         if(indices.size()==k){
            int x;
            if(k>0){
               x = indices.front();
               indices.pop();
            }
            else 
            x = i;
            res = max(res,i-low);
            low = x+1;
         }
         if(k>0)
         indices.push(i);
      }
      i++;
   }
   res = max(res,i-low);     
   return res;
} 
int main(){ 
   vector<int> arr = {0,0,1,0,1,1,0,1};
   int k = 1;
   cout<<Longest_subsegment_of_ones(arr,k)<<endl; 
   return 0; 
} 

Output

4

Time complexity − O(N), where N is the number of elements in the array.

Space complexity − O(k), where k is the number of changes we are allowed.

Updated on: 01-Nov-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements