Given a binary array arr[] of length N, and an integer K, the task is to find the maximum number of consecutive ones after flipping all zero in a subarray of length K.
Examples:
Input: arr[]= {0, 0, 1, 1, 1, 1, 0, 1, 1, 0}, K = 2
Output: 7
Explanation:
On taking the subarray [6, 7] and flip zero to one we get 7 consecutive ones.
Input: arr[]= {0, 0, 1, 1, 0, 0, 0, 0}, K = 3
Output: 5
Explanation:
On taking the subarray [4, 6] and flip zero to one we get 5 consecutive ones.
Approach: To solve the problem follow the steps given below:
- Initialize a variable let’s say trav which is going to iterate in the array from each position i to (0 to i-1) in the left direction and from (i+k to n-1) in the right direction.
- Check and keep an account that no zero comes in its way in any direction while iterating in the array.
- If there is a 0 then break out from the loop in that direction.
- So ultimately for i to i+k if there is any zero we are already flipping it to 1 so no need to count number of ones in this range as it will be equal to integer K only.
Below is the implementation of the above approach:
C++
// CPP program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum number of // consecutive 1's after flipping all // zero in a K length subarray int findmax(int arr[], int n, int k) { // Initialize variable int trav, i; int c = 0, maximum = 0; // Iterate unil n-k+1 as we // have to go till i+k for (i = 0; i < n - k + 1; i++) { trav = i - 1; c = 0; /*Iterate in the array in left direction till you get 1 else break*/ while (trav >= 0 && arr[trav] == 1) { trav--; c++; } trav = i + k; /*Iterate in the array in right direction till you get 1 else break*/ while (trav < n && arr[trav] == 1) { trav++; c++; } c += k; // Compute the maximum length if (c > maximum) maximum = c; } // Return the length return maximum; } // Driver code int main() { int k = 3; // Array initialization int arr[] = { 0, 0, 1, 1, 0, 0, 0, 0 }; // Size of array int n = sizeof arr / sizeof arr[0]; int ans = findmax(arr, n, k); cout << ans << '\n'; } |
Python3
# Python3 program for the above approach # Function to find the maximum number of # consecutive 1's after flipping all # zero in a K length subarray def findmax(arr, n, k): # Initialize variable trav, i = 0, 0 c = 0 maximum = 0 # Iterate unil n-k+1 as we # have to go till i+k while i < n - k + 1: trav = i - 1 c = 0 # Iterate in the array in left direction # till you get 1 else break while trav >= 0 and arr[trav] == 1: trav -= 1 c += 1 trav = i + k # Iterate in the array in right direction # till you get 1 else break while (trav < n and arr[trav] == 1): trav += 1 c += 1 c += k # Compute the maximum length if (c > maximum): maximum = c i += 1 # Return the length return maximum # Driver code if __name__ == '__main__': k = 3 # Array initialization arr = [0, 0, 1, 1, 0, 0, 0, 0] # Size of array n = len(arr) ans = findmax(arr, n, k) print(ans) # This code is contributed by Mohit Kumar |
5
Time complexity: O(N)
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.
Recommended Posts:
- Maximum subarray sum by flipping signs of at most K array elements
- Maximize number of 0s by flipping a subarray
- Maximum length of subarray such that all elements are equal in the subarray
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum length of subarray such that sum of the subarray is even
- Maximum sum subarray of even length
- Maximize distance between any two consecutive 1's after flipping M 0's
- Length of maximum product subarray
- Find subarray of Length K with Maximum Peak
- Find maximum average subarray of k length
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Find consecutive 1s of length >= n in binary representation of a number
- Length of longest consecutive zeroes in the binary representation of a number.
- Length of longest Subarray with equal number of odd and even elements
- Length of largest subarray whose all elements Powerful number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29

