✅ Three Equal Parts | Java | Easy to understand | Detailed approach
Intuition\n We can easily see that in order for the array to have equal parts, in each part the number of ones should be equal.\n The above implies that the tot
**Intuition**\n* We can easily see that in order for the array to have equal parts, in each part the number of ones should be equal.\n* The above implies that the `totalOnes` should be divisible by 3.\n* Now lets understand of basic cases\n* `totalOnes = 0`: If this is the case any two indices can divide into 3 equal parts since all will be zero\n* `totalOnes%3 !=0 `: Since total ones is not divisble solution is not possible, so we return `[-1,-1]`\n* Next we know that each part will have `k = totalOnes/3` ones. Also if we start from the end the target which we want for each part will start as soon as we have taken `k` ones. So we can keep track of this index from where target starts. ***Note***: This index might not be the index `j` as there can be leading zeroes which will not change the target.\n* Now we have to find if its possible to build the same target from start to the end.\n* Now to get the index `i` we can start from index `0` and move to the index of first `1` (What we do here is ignore leading zeroes). Now we start comparing elements from the target index and index i. If its equal then this part can be equal to target.\n* We do the similar operation for the second part.\n\n```\nclass Solution {\n    public int[] threeEqualParts(int[] arr) {\n        int totalOnes = 0;\n        for(int e:arr){\n            if(e==1){\n                totalOnes++;\n            }\n        }\n        \n        int l = arr.length;\n        if(totalOnes==0){\n            return new int[] {0, l-1};\n        }\n        if(totalOnes%3!=0){\n            return new int[] {-1,-1};\n        }\n        \n        int k=totalOnes/3; // Each part will have k number of 1\'s\n        int targetIdx = l; // This first index of \'1\' which starts the equal part\n        \n        while(k>0){\n            targetIdx--;\n            if(arr[targetIdx]==1){\n                k--;\n            }\n        }\n        \n        int i = isPartPossible(arr, 0, targetIdx);\n        if(i==-1){\n            return new int[] {-1,-1};\n        }\n        int j = isPartPossible(arr, i, targetIdx);\n        if(j==-1){\n            return new int[] {-1,-1};\n        }\n        \n        return new int[] {i-1,j};\n    }\n    \n    private int isPartPossible(int[] arr, int i, int start) {\n        while(arr[i]==0){\n            i++;\n        }\n        while(start<arr.length){\n            if(arr[i]!=arr[start]) {\n                return -1;\n            }\n            i++;\n            start++;\n        }\n        return i;\n    }\n}\n```\n\nPS: Please upvote if you think it can help others to understand this question.
['Java']