bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 927. Three Equal Parts

927. Three Equal Parts

Leetcode Solutions

Key approach of the solution: Equal Ones

  1. Count the total number of ones in the array (totalOnes).
  2. If totalOnes is not divisible by 3, return [-1, -1] as it is impossible to divide the array into three parts with equal binary values.
  3. Calculate targetOnes which is totalOnes / 3.
  4. Find the positions of the 1st, targetOnesth, (targetOnes+1)th, 2*targetOnesth, (2*targetOnes+1)th, and 3*targetOnesth ones in the array.
  5. These positions define three intervals: [i1, j1], [i2, j2], and [i3, j3].
  6. Count the number of trailing zeros after j3 (trailingZeros).
  7. Extend the intervals [i1, j1], [i2, j2] to include trailingZeros.
  8. If the intervals can be extended without overlap and the parts represented by these intervals are equal, return [j1+trailingZeros, j2+trailingZeros+1].
  9. Otherwise, return [-1, -1].
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...