Longest Subarray With Equal Number of 0s and 1s (original) (raw)
Last Updated : 13 Jan, 2025
Given an array **arr[] containing only **0s and **1s, find the longest subarray which contains equal no of **0s and **1s.
**Examples:
**Input: arr[] = [1, 0, 1, 1, 1, 0, 0]
**Output: 6
**Explanation: arr[1 ... 6] is the longest subarray with three 0s and three 1s.**Input: arr[] = [0, 0, 1, 1, 0]
**Output: 4
**Explanation: arr[0 ... 3] or arr[1 ... 4] is the longest subarray with two 0s and two 1s.**Input: arr[] = [0]
**Output: 0
**Explanation: There is no subarray with an equal number of 0s and 1s.
Table of Content
- [Naive Approach] Using Nested Loop - O(n^2) Time and O(1) Space
- [Expected Approach] Using Hashmap and Prefix Sum Technique - O(n) Time and O(n) Space
**[Naive Approach] Using Nested Loop - O(n^2) Time and O(1) Space
A simple approach is to generate all possible subarrays and check whether the subarray has equal number of **0s and **1s or not. To make this process easy we find **cumulative sum of the subarrays taking **0s as -1 and **1s **as +1. If the **cumulative sum is equal to 0 for any subarray then update the current maximum length with the maximum of length of current subarray and its own value.
C++ `
// C++ program to find the longest subarray with // equal number of 0s and 1s using nested loop
#include #include using namespace std;
int maxLen(vector &arr) { int res = 0;
// Pick a starting point as 's'
for (int s = 0; s < arr.size(); s++) {
int sum = 0;
// Consider all subarrays arr[s...e]
for (int e = s; e < arr.size(); e++) {
sum += (arr[e] == 0) ? -1 : 1;
// Check if it's a 0-sum subarray
if (sum == 0)
// update max size
res = max(res, e - s + 1);
}
}
return res;
}
int main() { vector arr = { 1, 0, 0, 1, 0, 1, 1 }; cout << maxLen(arr); }
C
// C program to find the longest subarray with // equal number of 0s and 1s using nested loop
#include <stdio.h>
int maxLen(int arr[], int n) { int res = 0;
// Pick a starting point as 's'
for (int s = 0; s < n; s++) {
int sum = 0;
// Consider all subarrays arr[s...e]
for (int e = s; e < n; e++) {
sum += (arr[e] == 0) ? -1 : 1;
// Check if it's a 0-sum subarray
if (sum == 0)
// Update max size
res = (res > e - s + 1) ? res : (e - s + 1);
}
}
return res;
}
int main() { int arr[] = {1, 0, 0, 1, 0, 1, 1}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d", maxLen(arr, n)); return 0; }
Java
// Java program to find the longest subarray with // equal number of 0s and 1s using nested loop
import java.util.*; class GfG { static int maxLen(int[] arr) { int res = 0;
// Pick a starting point as 's'
for (int s = 0; s < arr.length; s++) {
int sum = 0;
// Consider all subarrays arr[s...e]
for (int e = s; e < arr.length; e++) {
sum += (arr[e] == 0) ? -1 : 1;
// Check if it's a 0-sum subarray
if (sum == 0)
// Update max size
res = Math.max(res, e - s + 1);
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {1, 0, 0, 1, 0, 1, 1};
System.out.println(maxLen(arr));
}
}
Python
Python program to find the longest subarray with
equal number of 0s and 1s using nested loop
def maxLen(arr): res = 0
# Pick a starting point as 's'
for s in range(len(arr)):
sum = 0
# Consider all subarrays arr[s...e]
for e in range(s, len(arr)):
sum += -1 if arr[e] == 0 else 1
# Check if it's a 0-sum subarray
if sum == 0:
# Update max size
res = max(res, e - s + 1)
return res
if name == "main": array = [1, 0, 0, 1, 0, 1, 1]
print(maxLen(array))
C#
// C# program to find the longest subarray with // equal number of 0s and 1s using nested loop
using System; using System.Collections.Generic;
class GfG { static int maxLen(int[] arr) { int res = 0;
// Pick a starting point as 's'
for (int s = 0; s < arr.Length; s++) {
int sum = 0;
// Consider all subarrays arr[s...e]
for (int e = s; e < arr.Length; e++) {
sum += (arr[e] == 0) ? -1 : 1;
// Check if it's a 0-sum subarray
if (sum == 0)
// Update max size
res = Math.Max(res, e - s + 1);
}
}
return res;
}
static void Main() {
int[] array = { 1, 0, 0, 1, 0, 1, 1 };
Console.WriteLine(maxLen(array));
}
}
JavaScript
// JavaScript program to find the longest subarray with // equal number of 0s and 1s using nested loop
function maxLen(arr) { let res = 0;
// Pick a starting point as 's'
for (let s = 0; s < arr.length; s++) {
let sum = 0;
// Consider all subarrays arr[s...e]
for (let e = s; e < arr.length; e++) {
sum += arr[e] === 0 ? -1 : 1;
// Check if it's a 0-sum subarray
if (sum === 0) {
// Update max size
res = Math.max(res, e - s + 1);
}
}
}
return res;
}
// driver code let array = [1, 0, 0, 1, 0, 1, 1]; console.log(maxLen(array));
`
[Expected Approach] Using Hash Map and Prefix Sum Technique - O(n) Time and O(n) Space
If we consider every **0 as -1, then this problem become same as the longest subarray with 0 sum problem.
We traverse the array and compute the prefix sum
- If current **prefix sum == 0, it means that subarray from index ****(0)** till present index has equal number of **0's and 1's.
- If we encounter a prefix sum value which we have already encountered before, which means that subarray from the **previous index+1 till the **present index has equal number of 0's and 1's as they give a **cumulative sum of 0.
In a nutshell this problem is equivalent to finding two indexes **i & j in arr[], such that prefix sums till i and j are same, and (j - i) is maximum. To store the first occurrence of each unique cumulative sum value we use a **hash map where if we get that value again we can find the subarray size and compare it with the **maximum size found till now.
C++ `
// C++ program to find longest subarray with equal // number of 0's and 1's Using Hashmap and Prefix Sum
#include #include #include using namespace std;
int maxLen(vector &arr) { unordered_map<int, int> mp;
int preSum = 0;
int res = 0;
// Traverse through the given array
for (int i = 0; i < arr.size(); i++) {
// Add current element to sum
// if current element is zero, add -1
preSum += (arr[i] == 0) ? -1 : 1;
// To handle sum = 0 at last index
if (preSum == 0)
res = i + 1;
// If this sum is seen before, then update
// result with maximum
if (mp.find(preSum) != mp.end())
res = max(res, i - mp[preSum]);
// Else put this sum in hash table
else
mp[preSum] = i;
}
return res;
}
int main() { vector arr = {1, 0, 0, 1, 0, 1, 1};
cout << maxLen(arr) << endl;
return 0;
}
Java
// Java program to find longest subarray with equal // number of 0's and 1's Using HashMap and Prefix Sum import java.util.HashMap;
class GfG { static int maxLen(int[] arr) { HashMap<Integer, Integer> mp = new HashMap<>();
int preSum = 0;
int res = 0;
// Traverse through the given array
for (int i = 0; i < arr.length; i++) {
// Add current element to sum
// if current element is zero, add -1
preSum += (arr[i] == 0) ? -1 : 1;
// To handle sum = 0 at last index
if (preSum == 0)
res = i + 1;
// If this sum is seen before, then update
// result with maximum
if (mp.containsKey(preSum))
res = Math.max(res, i - mp.get(preSum));
// Else put this sum in hash table
else
mp.put(preSum, i);
}
return res;
}
public static void main(String[] args) {
int[] arr = {1, 0, 0, 1, 0, 1, 1};
System.out.println(maxLen(arr));
}
}
Python
Python program to find longest subarray with equal
number of 0's and 1's Using HashMap and Prefix Sum
def maxLen(arr): mp = {}
preSum = 0
res = 0
# Traverse through the given array
for i in range(len(arr)):
# Add current element to sum
# if current element is zero, add -1
preSum += -1 if arr[i] == 0 else 1
# To handle sum = 0 at last index
if preSum == 0:
res = i + 1
# If this sum is seen before, then update
# result with maximum
if preSum in mp:
res = max(res, i - mp[preSum])
# Else put this sum in hash table
else:
mp[preSum] = i
return res
if name == "main": arr = [1, 0, 0, 1, 0, 1, 1] print(maxLen(arr))
C#
// C# program to find longest subarray with equal // number of 0's and 1's Using Dictionary and Prefix Sum using System; using System.Collections.Generic;
class GfG { static int MaxLen(int[] arr) { Dictionary<int, int> mp = new Dictionary<int, int>();
int preSum = 0;
int res = 0;
// Traverse through the given array
for (int i = 0; i < arr.Length; i++) {
// Add current element to sum
// if current element is zero, add -1
preSum += (arr[i] == 0) ? -1 : 1;
// To handle sum = 0 at last index
if (preSum == 0)
res = i + 1;
// If this sum is seen before, then update
// result with maximum
if (mp.ContainsKey(preSum))
res = Math.Max(res, i - mp[preSum]);
// Else put this sum in hash table
else
mp[preSum] = i;
}
return res;
}
static void Main() {
int[] arr = {1, 0, 0, 1, 0, 1, 1};
Console.WriteLine(MaxLen(arr));
}
}
JavaScript
// JavaScript program to find longest subarray with equal // number of 0's and 1's Using HashMap and Prefix Sum
function maxLen(arr) { let mp = new Map();
let preSum = 0;
let res = 0;
// Traverse through the given array
for (let i = 0; i < arr.length; i++) {
// Add current element to sum
// if current element is zero, add -1
preSum += (arr[i] === 0) ? -1 : 1;
// To handle sum = 0 at last index
if (preSum === 0)
res = i + 1;
// If this sum is seen before, then update
// result with maximum
if (mp.has(preSum))
res = Math.max(res, i - mp.get(preSum));
// Else put this sum in hash table
else
mp.set(preSum, i);
}
return res;
}
// Driver Code const arr = [1, 0, 0, 1, 0, 1, 1]; console.log(maxLen(arr));
`
Similar Reads
- Hashing in Data Structure Hashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The 3 min read
- Introduction to Hashing Hashing refers to the process of generating a small sized output (that can be used as index in a table) from an input of typically large and variable size. Hashing uses mathematical formulas known as hash functions to do the transformation. This technique determines an index or location for the stor 7 min read
- What is Hashing? Hashing refers to the process of generating a fixed-size output from an input of variable size using the mathematical formulas known as hash functions. This technique determines an index or location for the storage of an item in a data structure. Need for Hash data structureThe amount of data on the 3 min read
- Index Mapping (or Trivial Hashing) with negatives allowed Index Mapping (also known as Trivial Hashing) is a simple form of hashing where the data is directly mapped to an index in a hash table. The hash function used in this method is typically the identity function, which maps the input data to itself. In this case, the key of the data is used as the ind 7 min read
- Separate Chaining Collision Handling Technique in Hashing Separate Chaining is a collision handling technique. Separate chaining is one of the most popular and commonly used techniques in order to handle collisions. In this article, we will discuss about what is Separate Chain collision handling technique, its advantages, disadvantages, etc.There are mainl 3 min read
- Open Addressing Collision Handling technique in Hashing Open Addressing is a method for handling collisions. In Open Addressing, all elements are stored in the hash table itself. So at any point, the size of the table must be greater than or equal to the total number of keys (Note that we can increase table size by copying old data if needed). This appro 7 min read
- Double Hashing Double hashing is a collision resolution technique used in hash tables. It works by using two hash functions to compute two different hash values for a given key. The first hash function is used to compute the initial hash value, and the second hash function is used to compute the step size for the 15+ min read
- Load Factor and Rehashing Prerequisites: Hashing Introduction and Collision handling by separate chaining How hashing works: For insertion of a key(K) - value(V) pair into a hash map, 2 steps are required: K is converted into a small integer (called its hash code) using a hash function.The hash code is used to find an index 15+ min read