> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Three Equal Parts Python Solution with Tests

> Tested Python solution for LeetCode 927 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 927, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/three-equal-parts/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 927   # by problem number
lcpy gen -s three_equal_parts   # by problem name
```

## Problem

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, and
* `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.

### Examples

```
Input: arr = [1,0,1,0,1]
Output: [0,3]
Explanation: The following figure shows a possible split of the array into three parts.
```

```
Input: arr = [1,1,0,1,1]
Output: [-1,-1]
```

```
Input: arr = [1,1,0,0,1]
Output: [0,2]
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/three_equal_parts/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/three_equal_parts/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) auxiliary (slices are short, each of length part_len)
    def three_equal_parts(self, arr: list[int]) -> list[int]:
        total_ones = arr.count(1)
        if total_ones % 3 != 0:
            return [-1, -1]
        if total_ones == 0:
            return [0, len(arr) - 1]

        target = total_ones // 3
        marks: list[int] = []
        seen = 0
        for idx, bit in enumerate(arr):
            if bit == 1:
                seen += 1
                if seen in (1, target + 1, 2 * target + 1):
                    marks.append(idx)
        first, second, third = marks

        part_len = len(arr) - third
        if arr[first : first + part_len] != arr[third:]:
            return [-1, -1]
        if arr[second : second + part_len] != arr[third:]:
            return [-1, -1]
        return [first + part_len - 1, second + part_len]
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) auxiliary (slices are short, each of length part\_len) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.