Maximize number of 0s by flipping a subarray
Given a binary array, find the maximum number zeros in an array with one flip of a subarray allowed. A flip operation switches all 0s to 1s and 1s to 0s.
Examples:
Input : arr[] = {0, 1, 0, 0, 1, 1, 0}
Output : 6
We can get 6 zeros by flipping the subarray {1, 1}
Input : arr[] = {0, 0, 0, 1, 0, 1}
Output : 5
A simple solution is to consider all subarrays and find a subarray with maximum value of (count of 1s) – (count of 0s). Let this value be max_diff. Finally return count of zeros in original array plus max_diff.
C++
// C++ program to maximize number of zeroes in a // binary array by at most one flip operation #include<bits/stdc++.h> using namespace std; // A Kadane's algorithm based solution to find maximum // number of 0s by flipping a subarray. int findMaxZeroCount(bool arr[], int n) { // Initialize max_diff = maximum of (Count of 0s - // count of 1s) for all subarrays. int max_diff = 0; // Initialize count of 0s in original array int orig_zero_count = 0; // Consider all Subarrays by using two nested two // loops for (int i=0; i<n; i++) { // Increment count of zeros if (arr[i] == 0) orig_zero_count++; // Initialize counts of 0s and 1s int count1 = 0, count0 = 0; // Consider all subarrays starting from arr[i] // and find the difference between 1s and 0s. // Update max_diff if required for (int j=i; j<n; j++) { (arr[j] == 1)? count1++ : count0++; max_diff = max(max_diff, count1 - count0); } } // Final result would be count of 0s in original // array plus max_diff. return orig_zero_count + max_diff; } // Driver program int main() { bool arr[] = {0, 1, 0, 0, 1, 1, 0}; int n = sizeof(arr)/sizeof(arr[0]); cout << findMaxZeroCount(arr, n); return 0; } |
Java
// JAVA Code for Maximize number of 0s by flipping // a subarray class GFG { // A Kadane's algorithm based solution to find maximum // number of 0s by flipping a subarray. public static int findMaxZeroCount(int arr[], int n) { // Initialize max_diff = maximum of (Count of 0s - // count of 1s) for all subarrays. int max_diff = 0; // Initialize count of 0s in original array int orig_zero_count = 0; // Consider all Subarrays by using two nested two // loops for (int i=0; i<n; i++) { // Increment count of zeros if (arr[i] == 0) orig_zero_count++; // Initialize counts of 0s and 1s int count1 = 0, count0 = 0; // Consider all subarrays starting from arr[i] // and find the difference between 1s and 0s. // Update max_diff if required for (int j = i; j < n; j ++) { if(arr[j] == 1) count1++; else count0++; max_diff = Math.max(max_diff, count1 - count0); } } // Final result would be count of 0s in original // array plus max_diff. return orig_zero_count + max_diff; } /* Driver program to test above function */ public static void main(String[] args) { int arr[] = {0, 1, 0, 0, 1, 1, 0}; System.out.println(findMaxZeroCount(arr, arr.length)); } } // This code is contributed by Arnav Kr. Mandal. |
Output:
6
This problem can be reduced to largest subarray sum problem. The idea is to consider every 0 as -1 and every 1 as 1, find the sum of largest subarray sum in this modified array. This sum is our required max_diff ( count of 0s – count of 1s in any subarray). Finally we return the max_diff plus count of zeros in original array.
C++
// C++ program to maximize number of zeroes in a // binary array by at most one flip operation #include<bits/stdc++.h> using namespace std; // A Kadane's algorithm based solution to find maximum // number of 0s by flipping a subarray. int findMaxZeroCount(bool arr[], int n) { // Initialize count of zeros and maximum difference // between count of 1s and 0s in a subarray int orig_zero_count = 0; // Initiale overall max diff for any subarray int max_diff = 0; // Initialize current diff int curr_max = 0; for (int i=0; i<n; i++) { // Count of zeros in original array (Not related // to Kadane's algorithm) if (arr[i] == 0) orig_zero_count++; // Value to be considered for finding maximum sum int val = (arr[i] == 1)? 1 : -1; // Update current max and max_diff curr_max = max(val, curr_max + val); max_diff = max(max_diff, curr_max); } max_diff = max(0, max_diff); return orig_zero_count + max_diff; } // Driver program int main() { bool arr[] = {0, 1, 0, 0, 1, 1, 0}; int n = sizeof(arr)/sizeof(arr[0]); cout << findMaxZeroCount(arr, n); return 0; } |
Java
// JAVA Code for Maximize number of 0s by // flipping a subarray class GFG { // A Kadane's algorithm based solution to find maximum // number of 0s by flipping a subarray. public static int findMaxZeroCount(int arr[], int n) { // Initialize count of zeros and maximum difference // between count of 1s and 0s in a subarray int orig_zero_count = 0; // Initiale overall max diff for any subarray int max_diff = 0; // Initialize current diff int curr_max = 0; for (int i = 0; i < n; i ++) { // Count of zeros in original array (Not related // to Kadane's algorithm) if (arr[i] == 0) orig_zero_count ++; // Value to be considered for finding maximum sum int val = (arr[i] == 1)? 1 : -1; // Update current max and max_diff curr_max = Math.max(val, curr_max + val); max_diff = Math.max(max_diff, curr_max); } max_diff = Math.max(0, max_diff); return orig_zero_count + max_diff; } /* Driver program to test above function */ public static void main(String[] args) { int arr[] = {0, 1, 0, 0, 1, 1, 0}; System.out.println(findMaxZeroCount(arr, arr.length)); } } // This code is contributed by Arnav Kr. Mandal. |
Output :
6
This article is contributed by Shivam Agrawal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Maximum subarray sum by flipping signs of at most K array elements
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximize the maximum subarray sum after removing atmost one element
- Minimum score after flipping the cards
- Maximize the number of subarrays with XOR as zero
- Maximize the number of sum pairs which are divisible by K
- Maximize array elements upto given number
- Flipping Sign Problem | Lazy Propagation Segment Tree
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Largest subarray with equal number of 0s and 1s
- Number of primes in a subarray (with updates)
- Queries for number of distinct elements in a subarray | Set 2
- Check in binary array the number represented by a subarray is odd or even
- Rearrange the array to maximize the number of primes in prefix sum of the array



