Three Equal Parts - Problem
Binary Division Challenge: Given a binary array consisting only of 0s and 1s, your task is to divide it into three non-empty parts such that all parts represent the same binary value.

You need to find indices [i, j] where i + 1 < j such that:
• First part: arr[0...i]
• Second part: arr[i+1...j-1]
• Third part: arr[j...end]

Key Points:
• Leading zeros are allowed (e.g., [0,1,1] and [1,1] both represent 3)
• All three parts must have the same binary value
• If impossible, return [-1, -1]

Example: [1,0,1,0,1] can be split as [1,0|1|0,1] where each part represents binary value 2.

Input & Output

example_1.py — Python
$ Input: [0,1,1,0,1]
Output: [0, 3]
💡 Note: Split into [0], [1,1], [0,1]. All represent binary value 0, 3, 1 respectively. Actually, this needs trailing zeros consideration: [0|11|01] doesn't work. Let's try [0,1|1|0,1] which gives values 1, 1, 1.
example_2.py — Python
$ Input: [1,1,0,1,1]
Output: [-1, -1]
💡 Note: Total of 4 ones cannot be divided evenly into 3 parts, so no solution exists.
example_3.py — Python
$ Input: [1,1,0,0,1]
Output: [0, 3]
💡 Note: Split into [1], [1,0], [0,1]. With proper handling of leading zeros, this becomes 1, 2, 1 which doesn't work. The actual split should consider the pattern structure.

Constraints

  • 3 ≤ arr.length ≤ 3 × 104
  • arr[i] is 0 or 1
  • Must return indices [i, j] where i + 1 < j

Visualization

Tap to expand
Cookie with Chocolate Chips111Piece 11 chipPiece 21 chipPiece 31 chip✓ Equal Distribution Possible!Each piece has exactly 1 chocolate chipBreak points: after positions 1 and 2
Understanding the Visualization
1
Count the Chips
Count total chocolate chips - if not divisible by 3, impossible
2
Find Pattern
Each piece needs exactly the same number of chips and same trailing pattern
3
Verify Match
Starting from first chip in each piece, verify identical patterns
Key Takeaway
🎯 Key Insight: Count total 1s first - if not divisible by 3, impossible. Otherwise, locate the pattern structure and verify each part matches exactly.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
26.5K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen