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

• Difficulty Level : Hard
• Last Updated : 11 Jul, 2022

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:

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.

Below is the implementation of the above approach:

C++

 `// C++ program for the above approach ` `#include ` `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; ` `} `

Java

 `// Java program for the above approach ` `import` `java.util.LinkedList; ` `import` `java.util.Queue; ` ` `  `class` `GFG{ ` ` `  `// Function to Find longest subsegment of 1s ` `static` `int` `get(``int` `n, ``int` `k, ``int` `arr[])  ` `{ ` `     `  `    ``// Queue for storing indices of 0s ` `    ``Queue q = ``new` `LinkedList(); ` ` `  `    ``int` `low = ``0``; ` `    ``int` `ans = Integer.MIN_VALUE; ` `    ``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.add(i); ` `            ``k--; ` `            ``i++; ` `        ``} ` `         `  `        ``// If k == 0 ` `        ``else` `        ``{ ` `             `  `            ``// Take out the index where ` `            ``// the first "0" was found ` `            ``int` `x = q.peek(); ` `            ``q.remove(); ` ` `  `            ``// Store the length as max ` `            ``// between i-low and that ` `            ``// of the previous answer ` `            ``ans = Math.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 = Math.max(ans, i - low); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `N = ``10``; ` `    ``int` `K = ``2``; ` `    ``int` `arr[] = { ``1``, ``0``, ``0``, ``1``, ``0``, ` `                  ``1``, ``0``, ``1``, ``0``, ``1` `}; ` ` `  `    ``System.out.println(get(N, K, arr)); ` `} ` `} ` ` `  `// This code is contributed by gfking`

Python3

 `# Python code for the above approach ` ` `  `# Function to Find longest subsegment of 1s ` `def` `get(n, k, arr): ` `   `  `    ``# Queue for storing indices of 0s ` `    ``q ``=` `[] ` ` `  `    ``low ``=` `0` `    ``ans ``=` `10` `*``*` `-``9` ` `  `    ``p ``=` `k ` `    ``i ``=` `0` ` `  `    ``while` `(i < n): ` `       `  `        ``# If the current character is 1 ` `        ``# then increment i by 1 ` `        ``if` `(arr[i] ``=``=` `1``): ` `            ``i ``+``=` `1` ` `  `        ``# If the current character is 0 ` `        ``# and some spells are ` `        ``# left then use them ` `        ``elif` `(arr[i] ``=``=` `0` `and` `k !``=` `0``): ` `            ``q.append(i) ` `            ``k ``-``=` `1` `            ``i ``+``=` `1` `        ``# If k == 0 ` `        ``else``: ` `            ``# Take out the index where ` `            ``# the first "0" was found ` `            ``x ``=` `q[``0``] ` `            ``q.pop(``0``) ` ` `  `            ``# 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 ``+``=` `1` ` `  `        ``# Store the length between ` `        ``# the i-low and that of ` `        ``# previous answer ` `        ``ans ``=` `max``(ans, i ``-` `low) ` `     `  `    ``return` `ans ` ` `  `# Driver Code ` `N ``=` `10` `K ``=` `2` `arr ``=` `[``1``, ``0``, ``0``, ``1``, ``0``, ``1``, ``0``, ``1``, ``0``, ``1``] ` `print``(get(N, K, arr)) ` ` `  `# This code is contributed by Saurabh Jaiswal`

C#

 `// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG { ` ` `  `  ``// Function to Find longest subsegment of 1s ` `  ``static` `int` `get``(``int` `n, ``int` `k, ``int``[] arr) ` `  ``{ ` ` `  `    ``// Queue for storing indices of 0s ` `    ``Queue<``int``> q = ``new` `Queue<``int``>(); ` ` `  `    ``int` `low = 0; ` `    ``int` `ans = Int32.MinValue; ` `    ``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.Enqueue(i); ` `        ``k--; ` `        ``i++; ` `      ``} ` ` `  `      ``// If k == 0 ` `      ``else` `{ ` ` `  `        ``// Take out the index where ` `        ``// the first "0" was found ` `        ``int` `x = q.Peek(); ` `        ``q.Dequeue(); ` ` `  `        ``// Store the length as max ` `        ``// between i-low and that ` `        ``// of the previous answer ` `        ``ans = Math.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 = Math.Max(ans, i - low); ` `    ``} ` `    ``return` `ans; ` `  ``} ` ` `  `  ``// Driver Code ` `  ``public` `static` `void` `Main() ` `  ``{ ` `    ``int` `N = 10; ` `    ``int` `K = 2; ` `    ``int``[] arr = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 1 }; ` ` `  `    ``Console.WriteLine(``get``(N, K, arr)); ` `  ``} ` `} ` ` `  `// This code is contributed by ukasp.`

Javascript

 ``

Output

`5`

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

Related Topic: Subarrays, Subsequences, and Subsets in Array

My Personal Notes arrow_drop_up
Recommended Articles
Page :