---

To solve this problem, we need to find the longest contiguous subarray in a binary array where the count of `0`s equals the count of `1`s.

Here’s the approach in a clear, step-by-step manner:

1. **Understand the Requirement**:
    - Given a binary array `nums` containing only `0`s and `1`s.
    - We need to find the maximum length of a contiguous subarray with an equal number of `0`s and `1`s.
2. **Example**:
    - **Input**: `nums = [0, 1, 0]`
    - **Output**: `2`
    - Explanation: The longest contiguous subarray with an equal number of `0`s and `1`s is `[0, 1]` or `[1, 0]`, both of length `2`.
3. **Using a Prefix Sum Approach**:
    - We can transform this problem using the concept of prefix sums:
        - Replace each `0` in the array with `1`.
        - Now, the task reduces to finding the longest subarray with a sum of `0`.
4. **Detailed Steps**:
    - Create a variable `sum_` initialized to `0` and a dictionary `prefix_sums` that maps each cumulative sum to its earliest index.
    - Traverse the array, updating the cumulative sum (`sum_`) as follows:
        - Add `1` for each `1` in `nums`.
        - Subtract `1` for each `0` in `nums` (treating `0` as `1`).
    - For each cumulative sum:
        - If this sum has been seen before in `prefix_sums`, it means the subarray between the previous index and the current index has a sum of `0`.
        - Calculate the length of this subarray and update the maximum length if it’s longer than the previously recorded one.
        - If this sum hasn’t been seen before, store it in `prefix_sums` with its index.
5. **Why This Works**:
    - By treating `0`s as `1`s, any contiguous subarray with an equal count of `0`s and `1`s will have a cumulative sum of `0`.

## Problem Statement

**Given**: A binary array `nums`, which consists of only `0`s and `1`s.

**Task**: Return the maximum length of a contiguous subarray that contains an equal number of `0`s and `1`s.

---

## Input and Output Formats

- **Input**: A list of integers (`List[int]`), where each integer is either `0` or `1`.
    
    ```python
    nums = [0, 1, 0, 1, 1, 1, 0]
    
    ```
    
- **Output**: An integer (`int`) representing the length of the longest contiguous subarray with equal numbers of `0`s and `1`s.
    
    ```python
    Output: 4
    
    ```
    

---

## Example

**Input**:

```python
nums = [0, 1]

```

**Output**:

```python
2

```

**Explanation**: The entire array `[0, 1]` has an equal number of `0`s and `1`s.

---

## Another Example

**Input**:

```python
nums = [0, 1, 0]

```

**Output**:

```python
2

```

**Explanation**: The longest contiguous subarrays with equal numbers of `0`s and `1`s are `[0, 1]` and `[1, 0]`, each of length `2`.

---

## Approach

To solve this problem systematically, we can use the **Prefix Sum** technique combined with a hash map to track the indices of cumulative sums. Here's how:

1. **Transform the Array**:
    - Replace all `0`s with `1`s. This transformation allows us to simplify the problem to finding a subarray with a sum of `0`.
    
    ```python
    transformed_nums = [-1 if num == 0 else 1 for num in nums]
    
    ```
    
2. **Compute the Prefix Sum Array**:
    - Create a prefix sum array where each element at index `i` represents the cumulative sum of the transformed array up to that index.
    
    ```python
    prefix_sum = []
    current_sum = 0
    for num in transformed_nums:
        current_sum += num
        prefix_sum.append(current_sum)
    
    ```
    
3. **Use a Hash Map to Track Indices of Prefix Sums**:
    - Initialize a hash map to store the first occurrence of each prefix sum.
    - Iterate through the prefix sum array. For each sum:
        - If the sum has been seen before, calculate the length of the subarray between the previous index and the current index.
        - Update the maximum length accordingly.
        - If the sum hasn't been seen, store the current index in the hash map.
    
    ```python
    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:
            max_length = max(max_length, i - sum_to_index[total])
        else:
            sum_to_index[total] = i
    
    ```
    
4. **Final Result**:
    - The variable `max_length` will hold the length of the longest contiguous subarray with equal numbers of `0`s and `1`s.
    
    ```python
    return max_length
    
    ```
    

---

## Detailed Example

Let's walk through a more detailed example to illustrate the approach.

**Input**:

```python
nums = [0, 1, 0, 1, 1, 1, 0]

```

**Step 1: Transform the Array**

```python
transformed_nums = [-1, 1, -1, 1, 1, 1, -1]

```

**Step 2: Compute the Prefix Sum Array**

```python
prefix_sum = [-1, 0, -1, 0, 1, 2, 1]

```

**Step 3: Use a Hash Map to Track Indices**

- Initialize `sum_to_index = {0: -1}` and `max_length = 0`.
- Iterate through `prefix_sum`:
    
    
    | Index (`i`) | Prefix Sum (`total`) | Action | `max_length` |
    | --- | --- | --- | --- |
    | 0 | -1 | Store `-1: 0` in `sum_to_index` | 0 |
    | 1 | 0 | Found `0` at index `-1`; subarray length `2` | 2 |
    | 2 | -1 | Found `-1` at index `0`; subarray length `2` | 2 |
    | 3 | 0 | Found `0` at index `-1`; subarray length `4` | 4 |
    | 4 | 1 | Store `1: 4` in `sum_to_index` | 4 |
    | 5 | 2 | Store `2: 5` in `sum_to_index` | 4 |
    | 6 | 1 | Found `1` at index `4`; subarray length `2` | 4 |

**Final Result**:

```python
max_length = 4

```

**Explanation**: The longest contiguous subarray with equal numbers of `0`s and `1`s is `[0, 1, 0, 1]`, which has a length of `4`.

---

## Summary

By transforming the binary array and utilizing a prefix sum approach with a hash map, we can efficiently determine the maximum length of a contiguous subarray containing equal numbers of `0`s and `1`s. This method ensures a time complexity of O(n), making it suitable for large input sizes.

---

**Q: What's the Role of the HashMap Here?**

The hashmap is used to track the first occurrence of each unique prefix sum, which helps us quickly calculate the length of subarrays with equal numbers of `0`s and `1`s.

**Q: How Do We Use the HashMap?**

We use it to store the index of each prefix sum as we compute it. This allows us to find the longest contiguous subarray by looking up the first occurrence of each prefix sum.

Here’s an example:

- **Input**:
    
    ```python
    nums = [0, 1, 0, 1, 1, 1, 0]
    
    ```
    
- **Step 1: Transform the Array**
    
    Replace each `0` with `-1` to make the calculation easier.
    
    ```python
    transformed_nums = [-1, 1, -1, 1, 1, 1, -1]
    
    ```
    
- **Step 2: Compute the Prefix Sum Array**
    
    Calculate the cumulative sum at each index.
    
    ```python
    prefix_sum = [-1, 0, -1, 0, 1, 2, 1]
    
    ```
    

As we can see, the prefix sum array `[-1, 0, -1, 0, 1, 2, 1]` repeats values where subarrays with an equal number of `0`s and `1`s exist.

**How Does the HashMap Help?**

- In the hashmap, we store each unique prefix sum as a key. The first time a prefix sum occurs, we save its index.
- This allows us to quickly calculate the length of any subarray by using the difference between the current index and the index of the first occurrence of that prefix sum.
- The hashmap thus helps us efficiently find the longest contiguous subarray with equal numbers of `0`s and `1`s.

Q- bahii honii 5alina n3mlo manual exectuion lel exmple h4a 

[0,1,0,1,1,0,0,1]