Longest subsegment of ‘1’s formed by changing at most k ‘0’s | Set 2 (Using Queue)
Last Updated :
07 Feb, 2023
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 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++
#include <bits/stdc++.h>
using namespace std;
int get( int n, int k, int arr[])
{
queue< int > q;
int low = 0;
int ans = INT_MIN;
int p = k;
int i = 0;
while (i < n) {
if (arr[i] == 1) {
i++;
}
else if (arr[i] == 0 && k != 0) {
q.push(i);
k--;
i++;
}
else {
int x = q.front();
q.pop();
ans = max(ans, i - low);
low = x + 1;
k++;
}
ans = max(ans, i - low);
}
return ans;
}
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
import java.util.LinkedList;
import java.util.Queue;
class GFG{
static int get( int n, int k, int arr[])
{
Queue<Integer> q = new LinkedList<Integer>();
int low = 0 ;
int ans = Integer.MIN_VALUE;
int i = 0 ;
while (i < n)
{
if (arr[i] == 1 )
{
i++;
}
else if (arr[i] == 0 && k != 0 )
{
q.add(i);
k--;
i++;
}
else
{
int x = q.peek();
q.remove();
ans = Math.max(ans, i - low);
low = x + 1 ;
k++;
}
ans = Math.max(ans, i - low);
}
return ans;
}
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));
}
}
|
Python3
def get(n, k, arr):
q = []
low = 0
ans = 10 * * - 9
p = k
i = 0
while (i < n):
if (arr[i] = = 1 ):
i + = 1
elif (arr[i] = = 0 and k ! = 0 ):
q.append(i)
k - = 1
i + = 1
else :
x = q[ 0 ]
q.pop( 0 )
ans = max (ans, i - low)
low = x + 1
k + = 1
ans = max (ans, i - low)
return ans
N = 10
K = 2
arr = [ 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 ]
print (get(N, K, arr))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int get ( int n, int k, int [] arr)
{
Queue< int > q = new Queue< int >();
int low = 0;
int ans = Int32.MinValue;
int i = 0;
while (i < n) {
if (arr[i] == 1) {
i++;
}
else if (arr[i] == 0 && k != 0) {
q.Enqueue(i);
k--;
i++;
}
else {
int x = q.Peek();
q.Dequeue();
ans = Math.Max(ans, i - low);
low = x + 1;
k++;
}
ans = Math.Max(ans, i - low);
}
return ans;
}
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));
}
}
|
Javascript
<script>
function get(n, k, arr) {
let q = [];
let low = 0;
let ans = Number.MIN_VALUE;
let p = k;
let i = 0;
while (i < n) {
if (arr[i] == 1) {
i++;
}
else if (arr[i] == 0 && k != 0) {
q.push(i);
k--;
i++;
}
else {
let x = q[0];
q.shift();
ans = Math.max(ans, i - low);
low = x + 1;
k++;
}
ans = Math.max(ans, i - low);
}
return ans;
}
let N = 10;
let K = 2;
let arr = [1, 0, 0, 1, 0,
1, 0, 1, 0, 1];
document.write(get(N, K, arr) + '<br>' );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(k)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Share your thoughts in the comments
Please Login to comment...