Clean Java solution, with O(1) space and O(n) complexity
\npublic class Solution {\n    public int[] threeEqualParts(int[] A) {\n        if (A == null || A.length < 3) return new int[]{-1, -1};\n        int oneCnt = 0
```\npublic class Solution {\n    public int[] threeEqualParts(int[] A) {\n        if (A == null || A.length < 3) return new int[]{-1, -1};\n        int oneCnt = 0;\n        for (int i = 0; i < A.length; i++) {\n            if (A[i] == 1) oneCnt++;\n        }\n        if (oneCnt % 3 != 0) return new int[]{-1, -1};\n        int oneAvg = oneCnt / 3;\n        if (oneAvg == 0) return new int[]{0, A.length - 1};\n\n        // find the start index for the 3rd part\n        int postOnes = 0, start3 = A.length - 1;\n        while (postOnes < oneAvg) {\n            if (A[start3--] == 1) postOnes++;\n        }\n        start3++;\n\n        // find the end index for the 1st part\n        int l = A.length - start3, i = 0;\n        while (A[i] == 0) i++;\n        for (int k = 0; k < l; k++) {\n            if (A[i + k] != A[start3 + k]) return new int[]{-1, -1};\n        }\n        int left = i + l - 1, j = left + 1;\n\n        // find the end index for the 2nd part\n        while (A[j] == 0) j++;\n        for (int k = 0; k < l; k++) {\n            if (A[k + j] != A[start3 + k]) return new int[]{-1, -1};\n        }\n        int right = j + l;\n\n        return new int[]{left, right};\n    }\n}\n```
[]