Open In App

Maximize number of 1s by flipping a subarray

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a binary array, the task is to find the maximum number of 1’s in the array with at most 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 : 5
Explanation: Flip the subarray from index 2 to 3. Array will become [0, 1, 1, 1, 1, 1, 0]. Number of 1’s will be 5.

Input : arr[] = [0, 0, 0, 1, 0, 1]
Output : 5
Explanation: Flip the subarray from index 0 to 2. Array will become [1, 1, 1, 1, 0, 1]. Number of 1’s will be 5.

Input : arr[] = [0, 0, 0, 0]
Output: 4
Explanation: Flip the complete array.

[Naive Approach] Using Two Nested Loops – O(n^2) time and O(1) space

The idea is to count the original number of 1’s in the array, then consider every possible subarray that could be flipped. For each potential subarray, we calculate how the flip would affect the total count of 1’s by subtracting the number of 1’s in the subarray (which would become 0’s after flipping) and adding the number of 0’s in the subarray (which would become 1’s after flipping).

C++
// C++ program to Maximize number of 1s by flipping a subarray
#include <bits/stdc++.h>
using namespace std;

int maxOnes(vector<int> &arr) {
    int n = arr.size();
    
    int oneCnt = 0;
    for (int i=0; i<n; i++) {
        if (arr[i] == 1) oneCnt++;
    }
    
    int ans = oneCnt;
    
    // Starting index of each subarray
    for (int i=0; i<n; i++) {
        int cnt = oneCnt;
        
        // Ending index of each subarray
        for (int j=i; j<n; j++) {
            
            // If we flip the values, 1's 
            // count will also change.
            if (arr[j] == 1) cnt--;
            else cnt++;
            
            // Compare with maximum answer
            ans = max(ans, cnt);
        }
    }
    
    return ans;
}

int main() {
    vector<int> arr = {0, 1, 0, 0, 1, 1, 0};
    cout << maxOnes(arr);
    return 0;
}
Java
// Java program to Maximize number of 1s by flipping a subarray

class GfG {

    static int maxOnes(int[] arr) {
        int n = arr.length;
        
        int oneCnt = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) oneCnt++;
        }
        
        int ans = oneCnt;
        
        // Starting index of each subarray
        for (int i = 0; i < n; i++) {
            int cnt = oneCnt;
            
            // Ending index of each subarray
            for (int j = i; j < n; j++) {
                
                // If we flip the values, 1's 
                // count will also change.
                if (arr[j] == 1) cnt--;
                else cnt++;
                
                // Compare with maximum answer
                ans = Math.max(ans, cnt);
            }
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {0, 1, 0, 0, 1, 1, 0};
        System.out.println(maxOnes(arr));
    }
}
Python
# Python program to Maximize number of 1s by flipping a subarray

def maxOnes(arr):
    n = len(arr)
    
    oneCnt = 0
    for i in range(n):
        if arr[i] == 1:
            oneCnt += 1
    
    ans = oneCnt
    
    # Starting index of each subarray
    for i in range(n):
        cnt = oneCnt
        
        # Ending index of each subarray
        for j in range(i, n):
            
            # If we flip the values, 1's 
            # count will also change.
            if arr[j] == 1:
                cnt -= 1
            else:
                cnt += 1
            
            # Compare with maximum answer
            ans = max(ans, cnt)
    
    return ans

if __name__ == "__main__":
    arr = [0, 1, 0, 0, 1, 1, 0]
    print(maxOnes(arr))
C#
// C# program to Maximize number of 1s by flipping a subarray
using System;

class GfG {

    static int maxOnes(int[] arr) {
        int n = arr.Length;
        
        int oneCnt = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) oneCnt++;
        }
        
        int ans = oneCnt;
        
        // Starting index of each subarray
        for (int i = 0; i < n; i++) {
            int cnt = oneCnt;
            
            // Ending index of each subarray
            for (int j = i; j < n; j++) {
                
                // If we flip the values, 1's 
                // count will also change.
                if (arr[j] == 1) cnt--;
                else cnt++;
                
                // Compare with maximum answer
                ans = Math.Max(ans, cnt);
            }
        }
        
        return ans;
    }

    static void Main() {
        int[] arr = {0, 1, 0, 0, 1, 1, 0};
        Console.WriteLine(maxOnes(arr));
    }
}
JavaScript
// JavaScript program to Maximize number of 1s by flipping a subarray

function maxOnes(arr) {
    let n = arr.length;
    
    let oneCnt = 0;
    for (let i = 0; i < n; i++) {
        if (arr[i] === 1) oneCnt++;
    }
    
    let ans = oneCnt;
    
    // Starting index of each subarray
    for (let i = 0; i < n; i++) {
        let cnt = oneCnt;
        
        // Ending index of each subarray
        for (let j = i; j < n; j++) {
            
            // If we flip the values, 1's 
            // count will also change.
            if (arr[j] === 1) cnt--;
            else cnt++;
            
            // Compare with maximum answer
            ans = Math.max(ans, cnt);
        }
    }
    
    return ans;
}

let arr = [0, 1, 0, 0, 1, 1, 0];
console.log(maxOnes(arr));

Output
5

[Expected Approach] Using Modified Kadane’ Algorithm – O(n) time and O(1) space

The idea is to transform the problem into a maximum subarray sum problem that can be solved with Kadane’s algorithm. By converting 1’s to -1 and 0’s to 1 in the original array, we reframe the problem—a 1 in the original array would decrease our count if flipped (hence -1), while a 0 would increase our count if flipped (hence 1). Applying Kadane’s algorithm to this transformed array gives us the maximum gain in 1’s possible through flipping any subarray. Adding this gain to the original count of 1’s yields the maximum possible number of 1’s achievable with at most one flip.

Step by step approach:

  1. Count the original number of 1’s in the array and transform the array by replacing 1’s with -1 and 0’s with 1.
  2. Apply Kadane’s algorithm to find the maximum sum subarray in the transformed array.
  3. This maximum sum represents the maximum net gain in the number of 1’s possible through flipping a subarray.
C++
// C++ program to Maximize number of 1s by flipping a subarray
#include <bits/stdc++.h>
using namespace std;

// Function to find the maximum sum 
// sub array.
int maxSumSub(vector<int> &arr) {
    int n = arr.size();
    
    int sum = 0;
    int ans = 0;
    
    for (int i=0; i<n; i++) {
        sum += arr[i];
        ans = max(ans, sum);
        
        // If subarray sum becomes negative
        // skip the subarray 
        if (sum < 0) sum = 0;
    }
    
    return ans;
}

// Main function to find the maximum number 
// of 1's by flipping a subarray.
int maxOnes(vector<int> &arr) {
    int n = arr.size();
    
    int oneCnt = 0;
    
    // Set 1's to -1 and 0's to 1 
    for (int i=0; i<n; i++) {
        if (arr[i] == 1) {
            arr[i] = -1;
            oneCnt++;
        }
        else arr[i] = 1;
    }
    
    // Apply kadane's algorithm to find 
    // the number of extra 1's 
    int maxDiff = maxSumSub(arr);
    
    // Return total 1's + flipped ones.
    return oneCnt + maxDiff;
}

int main() {
    vector<int> arr = {0, 1, 0, 0, 1, 1, 0};
    cout << maxOnes(arr);
    return 0;
}
Java
// Java program to Maximize number of 1s by flipping a subarray

class GfG {

    // Function to find the maximum sum 
    // sub array.
    static int maxSumSub(int[] arr) {
        int n = arr.length;
        
        int sum = 0;
        int ans = 0;
        
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            ans = Math.max(ans, sum);
            
            // If subarray sum becomes negative
            // skip the subarray 
            if (sum < 0) sum = 0;
        }
        
        return ans;
    }

    // Main function to find the maximum number 
    // of 1's by flipping a subarray.
    static int maxOnes(int[] arr) {
        int n = arr.length;
        
        int oneCnt = 0;
        
        // Set 1's to -1 and 0's to 1 
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) {
                arr[i] = -1;
                oneCnt++;
            } else arr[i] = 1;
        }
        
        // Apply kadane's algorithm to find 
        // the number of extra 1's 
        int maxDiff = maxSumSub(arr);
        
        // Return total 1's + flipped ones.
        return oneCnt + maxDiff;
    }

    public static void main(String[] args) {
        int[] arr = {0, 1, 0, 0, 1, 1, 0};
        System.out.println(maxOnes(arr));
    }
}
Python
# Python program to Maximize number of 1s by flipping a subarray

# Function to find the maximum sum 
# sub array.
def maxSumSub(arr):
    n = len(arr)
    
    sum = 0
    ans = 0
    
    for i in range(n):
        sum += arr[i]
        ans = max(ans, sum)
        
        # If subarray sum becomes negative
        # skip the subarray 
        if sum < 0:
            sum = 0
    
    return ans

# Main function to find the maximum number 
# of 1's by flipping a subarray.
def maxOnes(arr):
    n = len(arr)
    
    oneCnt = 0
    
    # Set 1's to -1 and 0's to 1 
    for i in range(n):
        if arr[i] == 1:
            arr[i] = -1
            oneCnt += 1
        else:
            arr[i] = 1
    
    # Apply kadane's algorithm to find 
    # the number of extra 1's 
    maxDiff = maxSumSub(arr)
    
    # Return total 1's + flipped ones.
    return oneCnt + maxDiff

if __name__ == "__main__":
    arr = [0, 1, 0, 0, 1, 1, 0]
    print(maxOnes(arr))
C#
// C# program to Maximize number of 1s by flipping a subarray
using System;

class GfG {

    // Function to find the maximum sum 
    // sub array.
    static int maxSumSub(int[] arr) {
        int n = arr.Length;
        
        int sum = 0;
        int ans = 0;
        
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            ans = Math.Max(ans, sum);
            
            // If subarray sum becomes negative
            // skip the subarray 
            if (sum < 0) sum = 0;
        }
        
        return ans;
    }

    // Main function to find the maximum number 
    // of 1's by flipping a subarray.
    static int maxOnes(int[] arr) {
        int n = arr.Length;
        
        int oneCnt = 0;
        
        // Set 1's to -1 and 0's to 1 
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) {
                arr[i] = -1;
                oneCnt++;
            } else arr[i] = 1;
        }
        
        // Apply kadane's algorithm to find 
        // the number of extra 1's 
        int maxDiff = maxSumSub(arr);
        
        // Return total 1's + flipped ones.
        return oneCnt + maxDiff;
    }

    static void Main() {
        int[] arr = {0, 1, 0, 0, 1, 1, 0};
        Console.WriteLine(maxOnes(arr));
    }
}
JavaScript
// JavaScript program to Maximize number of 1s by flipping a subarray

// Function to find the maximum sum 
// sub array.
function maxSumSub(arr) {
    let n = arr.length;
    
    let sum = 0;
    let ans = 0;
    
    for (let i = 0; i < n; i++) {
        sum += arr[i];
        ans = Math.max(ans, sum);
        
        // If subarray sum becomes negative
        // skip the subarray 
        if (sum < 0) sum = 0;
    }
    
    return ans;
}

// Main function to find the maximum number 
// of 1's by flipping a subarray.
function maxOnes(arr) {
    let n = arr.length;
    
    let oneCnt = 0;
    
    // Set 1's to -1 and 0's to 1 
    for (let i = 0; i < n; i++) {
        if (arr[i] === 1) {
            arr[i] = -1;
            oneCnt++;
        } else arr[i] = 1;
    }
    
    // Apply kadane's algorithm to find 
    // the number of extra 1's 
    let maxDiff = maxSumSub(arr);
    
    // Return total 1's + flipped ones.
    return oneCnt + maxDiff;
}

let arr = [0, 1, 0, 0, 1, 1, 0];
console.log(maxOnes(arr));

Output
5


Next Article
Article Tags :
Practice Tags :

Similar Reads