2.2 Zeeshan and Chandler's Ideaten Jump
Zeeshan and Chandler are competing in the thrilling "Ideaten Jump," a game where they must navigate through a series of binary jumps. Each jump is represented by either a 0 or a 1 on a long platform. However, to win the game, they need to identify the longest stretch on the platform where the number of 0s and 1s are equal. Zeeshan, known for his analytical skills, believes there’s an optimal way to solve this using advanced techniques. Chandler, with his quick thinking, agrees but challenges Zeeshan to prove it. Can you help Zeeshan find the length of the longest such subarray, so they can win the Ideaten Jump?
Constraints
The array will only contain binary values, i.e., 0s and 1s. The size of the array n will be between 1 and 100,000.
Input Format
The first line contains an integer n (1 ≤ n ≤ 10^5), the length of the binary array. The second line contains n space-separated integers, where each integer is either 0 or 1.
Output Format
Print a single integer representing the length of the longest subarray with an equal number of 0s and 1s.
Example 1
Input:
7
0 1 0 1 0 1 1
Output:
6
Explanation:
The longest subarray with an equal number of 0s and 1s is [0, 1, 0, 1, 0, 1], which has a length of 6.
Log In to solve the Question