Problem

Longest Equal Binary Subarray

FULLTIMEOA

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.

Examples
01 · Example 1
arr = [0,1,0,0,1,1,0]
return = 6

The subarray [1,0,0,1,1,0] has three 0s and three 1s.

02 · Example 2
arr = [0,0,1,1]
return = 4

The entire array is balanced.

03 · Example 3
arr = [1,1,1]
return = 0

No non-empty subarray has an equal number of 0s and 1s.

Constraints
  • 1 <= arr.length <= 100000
  • arr[i] is either 0 or 1
More HackerRank problems
drafts saved locally
public int longestEqualBinarySubarray(int[] arr) {
  // write your code here
}
arr[0,1,0,0,1,1,0]
expected6
sign in to submit