Problem

Longest Equal Binary Subarray

Learn this problem
HackerRank logoHackerRankFULLTIMEOA

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[]) → int

Examples

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.

Example 2

arr = [0,0,1,1]return = 4

The entire array is balanced.

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
checking account