Certainly! Let's perform a manual execution of the algorithm for the input array:
nums = [0, 1, 0, 1, 1, 0, 0, 1]
Our goal is to find the length of the longest contiguous subarray with an equal number of 0s and 1s.
Step 1: Transform the Array
First, we'll transform the original binary array by replacing each 0 with -1 and keeping 1 as is. This transformation helps in simplifying the problem to finding a subarray with a sum of 0.
transformed_nums = [-1 if num == 0 else 1 for num in nums]
Transformation:
| Index | Original nums | Transformed transformed_nums |
|---|
| 0 | 0 | -1 |
| 1 | 1 | 1 |
| 2 | 0 | -1 |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 0 | -1 |
| 6 | 0 | -1 |
| 7 | 1 | 1 |
Result:
transformed_nums = [-1, 1, -1, 1, 1, -1, -1, 1]
Step 2: Compute the Prefix Sum Array
Next, we'll compute the prefix sum array, where each element at index i represents the cumulative sum of the transformed_nums up to that index.
prefix_sum = []
current_sum = 0
for num in transformed_nums:
current_sum += num
prefix_sum.append(current_sum)
Calculation:
| Index | transformed_nums[i] | Cumulative Sum (current_sum) | prefix_sum |
|---|
| 0 | -1 | -1 | -1 |
| 1 | 1 | 0 | 0 |
| 2 | -1 | -1 | -1 |
| 3 | 1 | 0 | 0 |
| 4 | 1 | 1 | 1 |
| 5 | -1 | 0 | 0 |
| 6 | -1 | -1 | -1 |
| 7 | 1 | 0 | 0 |
Result:
prefix_sum = [-1, 0, -1, 0, 1, 0, -1, 0]
Step 3: Use a Hash Map to Track Prefix Sums
We'll utilize a hash map (sum_to_index) to store the first occurrence of each prefix sum. This allows us to quickly determine the length of a subarray with a sum of 0 by finding the distance between indices where the same prefix sum occurs.
sum_to_index = {0: -1} # Initialize with sum 0 at index -1
max_length = 0
for i, total in enumerate(prefix_sum):
if total in sum_to_index:
current_length = i - sum_to_index[total]
max_length = max(max_length, current_length)
else:
sum_to_index[total] = i
Detailed Iteration:
Index (i) | Prefix Sum (total) | Action | sum_to_index (after action) | current_length | max_length |
|---|
| - | - | Initialize | {0: -1} | - | 0 |
| 0 | -1 | Not in sum_to_index. Add -1: 0 | {0: -1, -1: 0} | - | 0 |
| 1 | 0 | Found 0 at index -1. Calculate length 1 - (-1) = 2 | {0: -1, -1: 0} | 2 | 2 |
| 2 | -1 | Found -1 at index 0. Calculate length 2 - 0 = 2 | {0: -1, -1: 0} | 2 | 2 |
| 3 | 0 | Found 0 at index -1. Calculate length 3 - (-1) = 4 | {0: -1, -1: 0} | 4 | 4 |
| 4 | 1 | Not in sum_to_index. Add 1: 4 | {0: -1, -1: 0, 1: 4} | - | 4 |
| 5 | 0 | Found 0 at index -1. Calculate length 5 - (-1) = 6 | {0: -1, -1: 0, 1: 4} | 6 | 6 |
| 6 | -1 | Found -1 at index 0. Calculate length 6 - 0 = 6 | {0: -1, -1: 0, 1: 4} | 6 | 6 |
| 7 | 0 | Found 0 at index -1. Calculate length 7 - (-1) = 8 | {0: -1, -1: 0, 1: 4} | 8 | 8 |
Explanation:
-
Index 0 (total = -1):
-1 is not in sum_to_index.
- Add
-1: 0 to sum_to_index.
max_length remains 0.
-
Index 1 (total = 0):
0 is found in sum_to_index at index -1.
- Subarray length:
1 - (-1) = 2.
- Update
max_length to 2.
-
Index 2 (total = -1):
-1 is found in sum_to_index at index 0.
- Subarray length:
2 - 0 = 2.
max_length remains 2.
-
Index 3 (total = 0):
0 is found in sum_to_index at index -1.
- Subarray length:
3 - (-1) = 4.
- Update
max_length to 4.
-
Index 4 (total = 1):
1 is not in sum_to_index.
- Add
1: 4 to sum_to_index.
max_length remains 4.
-
Index 5 (total = 0):
0 is found in sum_to_index at index -1.
- Subarray length:
5 - (-1) = 6.
- Update
max_length to 6.
-
Index 6 (total = -1):
-1 is found in sum_to_index at index 0.
- Subarray length:
6 - 0 = 6.
max_length remains 6.
-
Index 7 (total = 0):
Step 4: Final Result
After completing the iteration, the maximum length of a contiguous subarray with equal numbers of 0s and 1s is:
max_length = 8
Conclusion: The entire array [0, 1, 0, 1, 1, 0, 0, 1] has an equal number of 0s and 1s (4 of each), and thus the longest contiguous subarray satisfying the condition is of length 8.
Summary
By following the prefix sum approach combined with a hash map, we efficiently determined that the longest contiguous subarray with equal numbers of 0s and 1s in the input [0, 1, 0, 1, 1, 0, 0, 1] has a length of 8. This method ensures a time complexity of O(n), making it suitable for large datasets.