Largest Subarray - C++ Solution

1. Introduction

The "Largest Subarray" problem in binary arrays is a classic challenge in algorithm design. It involves finding the largest contiguous subarray with an equal number of 0’s and 1’s. This problem tests the programmer's skills in array manipulation, hashing, and understanding of subarray concepts.

Problem

Given a binary array consisting of 0's and 1's, the objective is to find the largest contiguous subarray that contains an equal number of 0's and 1's. If multiple such subarrays exist, any one of them can be returned. If no such subarray exists, the output should be an empty array.

2. Solution Steps

1. Replace all 0’s in the array with -1. This transforms the problem into finding the largest subarray with a sum of 0.

2. Keep a cumulative sum of the elements as we traverse the array.

3. Use a hash map to store the first occurrence of each cumulative sum.

4. As we calculate the cumulative sum, check if the sum has been seen before.

5. If it has, the subarray from the previous occurrence of this sum to the current index is a candidate.

6. Keep track of the length of the largest such subarray found.

3. Code Program

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

// Function to find the largest subarray with equal number of 0's and 1's
vector<int> largestSubarray(vector<int> &nums) {
    int sum = 0, maxLength = 0, endIndex = -1;
    unordered_map<int, int> sumMap; // Map to store the first occurrence of each sum

    for (int i = 0; i < nums.size(); i++) {
        nums[i] = (nums[i] == 0) ? -1 : 1; // Replace 0’s with -1
        sum += nums[i];

        // Check if the sum is 0, indicating a valid subarray from the start
        if (sum == 0) {
            maxLength = i + 1;
            endIndex = i;
        }

        // Check if the sum is seen before
        if (sumMap.find(sum) != sumMap.end()) {
            if (maxLength < i - sumMap[sum]) {
                maxLength = i - sumMap[sum];
                endIndex = i;
            }
        } else {
            sumMap[sum] = i; // Store first occurrence of this sum
        }
    }

    if (endIndex != -1) {
        return vector<int>(nums.begin() + endIndex - maxLength + 1, nums.begin() + endIndex + 1);
    } else {
        return {};
    }
}

int main() {
    vector<int> nums = {0, 0, 1, 0, 1, 0, 0};
    vector<int> result = largestSubarray(nums);
    // Replace -1 back with 0 for output
    for (int &num : result) {
        num = (num == -1) ? 0 : 1;
    }
    for (int num : result) {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}

Output:

0 1 0 1

Explanation:

The program first converts the problem into finding the largest subarray with a sum of 0 by replacing 0's with -1. 

Then, it uses a hash map to keep track of the first occurrence of each cumulative sum. If the same sum appears again, it means we have found a subarray with equal numbers of 0’s and 1’s. The algorithm checks for the largest such subarray and returns it. 

This approach efficiently solves the problem with linear time complexity and offers an elegant way to tackle similar problems involving subarrays.


Comments