Given an array arr of 0s and 1s. Find and return the length of the longest subarray with equal number of 0s and 1s.
Example 1
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.
Example 2
Input: arr[] = [0, 0, 1, 1, 0]
Output: 4 Explnation: arr[0...3] or arr[1...4] is the longest subarray with two 0s and two 1s.
Example 3
Input: arr[] = [0]
Output: 0 Explnation: There is no subarray with an equal number of 0s and 1s.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
0 0 1 1 0
4
1 0 1 1 1 0 0
6