The Wayback Machine - https://web.archive.org/web/20211222143259/https://www.geeksforgeeks.org/longest-subsegment-of-1s-formed-by-changing-at-most-k-0s-set-2-using-queue/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 22 Dec, 2021

Given a binary array a[] and a number k, we need to find the length of the longest subsegment of ‘1’s possible by changing at most k ‘0’s.

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: a[] = {1, 0, 0, 1, 1, 0, 1},  k = 1
Output: 4
Explanation: Here, we should only change 1 zero(0). Maximum possible length we can get is by changing the 3rd zero in the array, we get a[] = {1, 0, 0, 1, 1, 1, 1}



Input: a[] = {1, 0, 0, 1, 0, 1, 0, 1, 0, 1}, k = 2
Output: 5 

 

Two Pointer Approach: Refer the Set 1 of this article for the implementation of Two-pointer approach.

Queue Approach: The task can be solved with the help of a queue. Store the indices of 0s encountered so far in a queue. For each 0, check if the value of K is greater than 0 or not, if it is non-zero, flip it to 1, and maximize the subsegment length correspondingly, else shift the left pointer (initially at the start index of the string) to the index of first zero (queue’s front) + 1.
Follow the below steps to solve the problem:

  • Declare a queue for storing Indices of 0s Visited.
  • Iterate over the string and if  If the current character is 0 and some spells are left i.e. (k != 0) then use the spell i.e. (decrement k). Also, store the index of “0” occurred.
  • If k = 0, Take out the front of the queue and store it in a variable.
  • Store the length as max between i-low and that of the previous answer.
  • Shift low to index of first “0” + 1 and increment k.
  • Finally, return the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to Find longest subsegment of 1s
int get(int n, int k, int arr[])
{
    // Queue for storing indices of 0s
    queue<int> q;
  
    int low = 0;
    int ans = INT_MIN;
  
    int p = k;
    int i = 0;
  
    while (i < n) {
        // If the current character is 1
        // then increment i by 1
        if (arr[i] == 1) {
            i++;
        }
  
        // If the current character is 0
        // and some spells are
        // left then use them
        else if (arr[i] == 0 && k != 0) {
            q.push(i);
            k--;
            i++;
        }
        // If k == 0
        else {
            // Take out the index where
            // the first "0" was found
            int x = q.front();
            q.pop();
  
            // Store the length as max
            // between i-low and that
            // of the previous answer
            ans = max(ans, i - low);
  
            // Shift low to index
            // of first "O" + 1
            low = x + 1;
  
            // Increase spell by 1
            k++;
        }
  
        // Store the length between
        // the i-low and that of
        // previous answer
        ans = max(ans, i - low);
    }
    return ans;
}
  
// Driver Code
int main()
{
    int N = 10;
    int K = 2;
    int arr[] = { 1, 0, 0, 1, 0,
                  1, 0, 1, 0, 1 };
  
    cout << get(N, K, arr) << endl;
    return 0;
}
Output
5

Time Complexity: O(N)
Auxiliary Space: O(k)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!