FastPrepLongest Equal-Zero-One Subarray

Longest Equal-Zero-One Subarray

Motive logoMotive● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given a binary array nums, return the maximum length of a contiguous subarray containing the same number of 0s and 1s.

Function

longestBalancedSubarray(nums: int[]) → int

Examples

Example 1

nums = [0,1]return = 2

The full array contains one zero and one one.

Example 2

nums = [0,1,0]return = 2

Either length-two prefix is balanced.

Constraints

  • 0 <= nums.length <= 100000.
  • Every value is 0 or 1.

More Motive problems

See Motive hiring insights
public int longestBalancedSubarray(int[] nums) {
  // write your code here
}
nums[0,1]
expected2
Checking account…