Longest Equal Binary Subarray
Updated
Problem statement
You are given a binary array arr containing only 0s and 1s.
Return the length of the longest contiguous subarray that contains an equal number of 0s and 1s.
Function
longestEqualBinarySubarray(arr: int[]) → intExamples
Example 1
arr = [0,1,0,0,1,1,0]return = 6The subarray [1,0,0,1,1,0] has three 0s and three 1s.
Example 2
arr = [0,0,1,1]return = 4The entire array is balanced.
Example 3
arr = [1,1,1]return = 0No non-empty subarray has an equal number of 0s and 1s.
Constraints
1 <= arr.length <= 100000arr[i]is either0or1
More HackerRank interview questions
- Longest Equal Binary Subarray After One FlipOA · Seen Dec 2025
- Longest Prefix-Valid Binary SubarrayOA · Seen May 2025
- Classify Password StrengthPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Check Permutation Divisible by EightONSITE INTERVIEW · Seen Mar 2026
- Ball Passing at Time KPHONE SCREEN · Seen Dec 2025