Logical Thinking
What is \'same binary value\' like? \n> Digits following first 1 in each part should be identical. \n\n> How could we decide which 1 is the first 1 of a divided
> What is \'same binary value\' like? \n> Digits following first 1 in each part should be identical. \n\n> How could we decide which 1 is the first 1 of a divided part?\n> \n> Assuming `numOnes` is number of 1s in A, each part should be with `numOnesPerPart = numOnes / 3` 1s. \n> So the first \'1\' in  left part is `1`st \'1\', first \'1\' in middle part is `numOnesPerPart + 1`th \'1\', first \'1\' in right part is `numOnesPerPart * 2 + 1`th \'1\'.\n>\n> Then we compare the rest part digit by digit. Hopefully, the pointer in right part will reach A.length, which means we can make three equal parts.\n> \n>There is a corner case such as all elements in A are 0s.\n****\n```\nclass Solution {\n    public int[] threeEqualParts(int[] A) {\n        int numOnes = countOnes(A);\n        if (numOnes % 3 != 0) return new int[]{-1, -1};\n        else {\n            int numOnesPerPart = numOnes / 3;\n            \n            // Set pointers at first one in each part.\n            int leftPtr = locateOne(A, 1), midPtr = locateOne(A, numOnesPerPart + 1), rightPtr = locateOne(A, numOnesPerPart * 2 + 1);\n            \n            // Compare 3 parts digit by digit.\n            while (leftPtr < midPtr && midPtr < rightPtr && rightPtr < A.length) {\n                if (A[leftPtr] == A[midPtr] && A[rightPtr] == A[midPtr]) {\n                    leftPtr++;\n                    midPtr++;\n                    rightPtr++;\n                } else break;\n            }\n            \n            if (rightPtr == A.length) return new int[]{leftPtr - 1, midPtr};\n            else if (rightPtr == 0) return new int[]{0, A.length - 1};\n            else return new int[]{-1, -1};\n        }\n    }\n    \n    private int countOnes(int[] A) {\n        int numOnes = 0;\n        for (int i = 0; i < A.length; i++) {\n            if (A[i] == 1) {\n                numOnes++;\n            }\n        }\n        return numOnes;        \n    }\n    \n    private int locateOne(int[] A, int targetOne) {\n        int cntOne = 0;\n        for (int i = 0; i < A.length; i++) {\n            if (A[i] == 1) {\n                cntOne++;\n                if (cntOne == targetOne) return i;\n            }\n        }\n        return 0; // When all elements in A are 0.\n    }\n}\n```\n**(\u4EBA \u2022\u0348\u1D17\u2022\u0348)** Thanks for voting!
[]