Three Equal Parts

You are given an array arr which consists of only zeros and ones. Divide the array into three non-empty parts such that all of these parts represent the same binary value.

If it is possible, return any [i, j] with i + 1 < j, such that:

  • arr[0], arr[1], ..., arr[i] is the first part,
  • arr[i + 1], arr[i + 2], ..., arr[j - 1] is the second part,
  • arr[j], arr[j + 1], ..., arr[arr.length - 1] is the third part,
  • all three parts have equal binary values.

If it is not possible, return [-1, -1].

Note that the entire part is used when considering what binary value it represents. For example, [1, 1, 0] represents 6 in decimal, not 3. Also, leading zeros are allowed, so [0, 1, 1] and [1, 1] represent the same value.

Example 1
Inputarr = [1,0,1,0,1]
Output[0,3]
Splitting after index 0 and starting the third part at index 3 gives parts [1], [0, 1], and [0, 1], which all represent the same binary value.
Example 2
Inputarr = [1,1,0,1,1]
Output[-1,-1]
There is no way to split the array into three non-empty parts that represent the same binary value.

Constraints

  • 3 <= arr.length <= 3 * 10^4
  • arr[i] is 0 or 1

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate