Skip to content

Commit ca60026

Browse files
authored
Merge pull request #1391 from nishant0708/Q525
[Feature Request]: Leetcode Q525 #1339
2 parents 35c3bee + 4696dec commit ca60026

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
id: contiguous-array
3+
title: Contiguous Array
4+
sidebar_label: 0525-Contiguous-Array
5+
tags:
6+
- Array
7+
- Hash Table
8+
description: "Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1."
9+
---
10+
11+
## Problem
12+
13+
Given a binary array `nums`, return the maximum length of a contiguous subarray with an equal number of `0` and `1`.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
19+
**Input:** `nums = [0,1]`
20+
**Output:** `2`
21+
**Explanation:** [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
22+
23+
**Example 2:**
24+
25+
**Input:** `nums = [0,1,0]`
26+
**Output:** `2`
27+
**Explanation:** [0, 1] (or [1, 0]) is the longest contiguous subarray with an equal number of 0 and 1.
28+
29+
### Constraints
30+
31+
- `1 <= nums.length <= 10^5`
32+
- `nums[i]` is either `0` or `1`.
33+
34+
---
35+
36+
## Approach
37+
38+
To solve this problem, we can use a hash map to keep track of the cumulative sum of the transformed array where `0` is replaced by `-1` and `1` is kept as `1`. The idea is to find the subarray with a cumulative sum of zero.
39+
40+
### Steps:
41+
42+
1. Initialize a hash map to store the cumulative sum and its corresponding index.
43+
2. Initialize `cumulative_sum` to `0` and `max_length` to `0`.
44+
3. Traverse the array:
45+
- Replace `0` with `-1`.
46+
- Update the `cumulative_sum`.
47+
- If the `cumulative_sum` is `0`, update `max_length` to the current index + 1.
48+
- If the `cumulative_sum` has been seen before, calculate the subarray length and update `max_length` if it's larger than the current `max_length`.
49+
- If the `cumulative_sum` has not been seen before, store it in the hash map with the current index.
50+
4. Return `max_length`.
51+
52+
### Solution
53+
54+
#### Java
55+
56+
```java
57+
import java.util.HashMap;
58+
59+
class Solution {
60+
public int findMaxLength(int[] nums) {
61+
HashMap<Integer, Integer> map = new HashMap<>();
62+
map.put(0, -1);
63+
int maxLength = 0, cumulativeSum = 0;
64+
65+
for (int i = 0; i < nums.length; i++) {
66+
cumulativeSum += (nums[i] == 0) ? -1 : 1;
67+
if (map.containsKey(cumulativeSum)) {
68+
maxLength = Math.max(maxLength, i - map.get(cumulativeSum));
69+
} else {
70+
map.put(cumulativeSum, i);
71+
}
72+
}
73+
74+
return maxLength;
75+
}
76+
}
77+
```
78+
#### C++
79+
80+
```cpp
81+
#include <vector>
82+
#include <unordered_map>
83+
using namespace std;
84+
85+
class Solution {
86+
public:
87+
int findMaxLength(vector<int>& nums) {
88+
unordered_map<int, int> map;
89+
map[0] = -1;
90+
int maxLength = 0, cumulativeSum = 0;
91+
92+
for (int i = 0; i < nums.size(); i++) {
93+
cumulativeSum += (nums[i] == 0) ? -1 : 1;
94+
if (map.find(cumulativeSum) != map.end()) {
95+
maxLength = max(maxLength, i - map[cumulativeSum]);
96+
} else {
97+
map[cumulativeSum] = i;
98+
}
99+
}
100+
101+
return maxLength;
102+
}
103+
};
104+
```
105+
#### Python
106+
107+
```python
108+
class Solution:
109+
def findMaxLength(self, nums: List[int]) -> int:
110+
count_map = {0: -1}
111+
max_length = 0
112+
cumulative_sum = 0
113+
114+
for i, num in enumerate(nums):
115+
cumulative_sum += -1 if num == 0 else 1
116+
if cumulative_sum in count_map:
117+
max_length = max(max_length, i - count_map[cumulative_sum])
118+
else:
119+
count_map[cumulative_sum] = i
120+
121+
return max_length
122+
```
123+
### Complexity Analysis
124+
**Time Complexity:** O(n)
125+
>Reason: We traverse the array once, and each lookup or insertion operation in the hash map is O(1) on average.
126+
127+
**Space Complexity:** O(n)
128+
>Reason: In the worst case, we may store up to n entries in the hash map.
129+
130+
### References
131+
**LeetCode Problem:** Contiguous Array

0 commit comments

Comments
 (0)