Contiguous Array - Complete Solution Guide
Contiguous Array is LeetCode problem 525, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a binary array nums , return the maximum length of a contiguous subarray with an equal number of 0 and 1 . Example 1: Input: nums = [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. Example 2: Input: nums = [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. Example 3: Input: nums = [0,1,1,1,1,1,0,0,0] Output: 6 Explanation: [1,1,1,0,0,0] is the longest contiguous subarray wit
Detailed Explanation
The problem asks us to find the maximum length of a contiguous subarray within a given binary array (an array containing only 0s and 1s) that has an equal number of 0s and 1s. For example, in the array [0, 1, 0, 1], the subarrays [0, 1] and [1, 0] each have an equal number of 0s and 1s, and their length is 2. The subarray [0, 1, 0, 1] also has equal 0s and 1s and has a length of 4. The goal is to find the *longest* such subarray. The input is an array `nums` of 0s and 1s, and the output is the maximum length of a contiguous subarray with an equal number of 0s and 1s.
Solution Approach
The solution uses a prefix sum approach combined with a hash table. We iterate through the array, maintaining a running 'count'. Each time we encounter a 1, we increment the count. Each time we encounter a 0, we decrement the count (treat it as -1). We store the first occurrence of each count value in a hash table, along with its index. When we encounter the same count value again at a later index, it means the subarray between the current index and the index stored in the hash table sums to 0 (equal number of 0s and 1s). We then update the maximum length accordingly.
Step-by-Step Algorithm
- Step 1: Initialize a hash map `count_map` to store the first occurrence of each count, with an initial entry `{0: -1}`. This entry accounts for subarrays starting from the beginning of the array.
- Step 2: Initialize `max_len` to 0 to store the maximum length found so far.
- Step 3: Initialize `count` to 0 to store the running count.
- Step 4: Iterate through the input array `nums` using a `for` loop and index `i`.
- Step 5: For each element `num` in `nums`, update the `count`. If `num` is 1, increment `count`; otherwise (if `num` is 0), decrement `count`.
- Step 6: Check if `count` is already present in the `count_map`.
- Step 7: If `count` is in `count_map`, it means we have encountered this prefix sum before. Calculate the length of the subarray between the current index `i` and the index stored in `count_map[count]`. Update `max_len` with the maximum of the current `max_len` and the calculated length.
- Step 8: If `count` is not in `count_map`, store the current `count` and its index `i` in the `count_map`.
- Step 9: After iterating through the entire array, return `max_len`.
Key Insights
- Insight 1: Instead of directly counting 0s and 1s in subarrays, we can transform the 0s into -1s. Then, the problem becomes finding the longest subarray whose sum is 0.
- Insight 2: We can use a hash table (or map) to store the prefix sum at each index. If we encounter the same prefix sum again at a later index, it means that the subarray between those two indices has a sum of 0.
- Insight 3: Initialize the hash map with {0: -1}. This is to handle the case where a subarray starting from index 0 has an equal number of 0s and 1s.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Prefix Sum.
Companies
Asked at: Morgan Stanley.