1 ms, faster than 100.00% of Java online submissions . O(n)
\nclass Solution {\n    public int[] threeEqualParts(int[] A) {\n        int l=A.length;\n        int ones=0;\n        int[] count=new int[l];\n        for(int 
```\nclass Solution {\n    public int[] threeEqualParts(int[] A) {\n        int l=A.length;\n        int ones=0;\n        int[] count=new int[l];\n        for(int i=0;i<l;i++){\n            ones=(A[i]==1)?ones+1:ones;\n            count[i]=ones;\n        }\n        \n        int[] ans=new int[2];\n        ans[0]=-1;\n        ans[1]=-1;\n        if(ones==0){\n            ans[0]=0;\n            ans[1]=2;\n            return ans;\n        }\n        if(ones%3!=0){\n            return ans;\n        }\n        int i=l-1;\n        while(i>=0 && A[i]!=1){\n            i--;\n        }\n        \n        int endzeroes=l-1-i;\n        \n        i=binarySearch(count,ones/3)+endzeroes;\n        int j=binarySearch(count,2*ones/3)+endzeroes+1;\n        int firststart=0;\n        int secondstart=i+1;\n        int thirdstart=j;\n        \n        while(firststart<=i && A[firststart]==0){\n            firststart++;\n        }\n        while(secondstart<j && A[secondstart]==0){\n            secondstart++;\n        }\n        while(thirdstart<l && A[thirdstart]==0){\n            thirdstart++;\n        }\n        \n        boolean possible=true;\n        for(int r=firststart,s=secondstart,t=thirdstart;r<=i && s<j && t<l;r++,s++,t++){\n            possible=possible && (A[r]==A[s] && A[s]==A[t]);\n        }\n        if(!possible || i-firststart+1!=j-secondstart || j-secondstart!=l-thirdstart){\n            return ans;\n        }\n        ans[0]=i;\n        ans[1]=j;\n        return ans;\n    }\n    public int binarySearch(int[] A,int val){\n        int l=0;\n        int r=A.length-1;\n        while(l<=r){\n            int mid=(l+r)/2;\n            if((mid==0 || A[mid-1]<val) && A[mid]==val){\n                return mid;\n            }\n            else if(val>A[mid]){\n                l=mid+1;\n            }\n            else{\n                r=mid-1;\n            }\n        }\n        return -1;\n    }\n}\n```
[]