<p>You are given an array <code>arr</code> which consists of only zeros and ones, divide the array into <strong>three non-empty parts</strong> such that all of these parts represent the same binary value.</p>

<p>If it is possible, return any <code>[i, j]</code> with <code>i + 1 &lt; j</code>, such that:</p>

<ul>
	<li><code>arr[0], arr[1], ..., arr[i]</code> is the first part,</li>
	<li><code>arr[i + 1], arr[i + 2], ..., arr[j - 1]</code> is the second part, and</li>
	<li><code>arr[j], arr[j + 1], ..., arr[arr.length - 1]</code> is the third part.</li>
	<li>All three parts have equal binary values.</li>
</ul>

<p>If it is not possible, return <code>[-1, -1]</code>.</p>

<p>Note that the entire part is used when considering what binary value it represents. For example, <code>[1,1,0]</code> represents <code>6</code> in decimal, not <code>3</code>. Also, leading zeros <strong>are allowed</strong>, so <code>[0,1,1]</code> and <code>[1,1]</code> represent the same value.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> arr = [1,0,1,0,1]
<strong>Output:</strong> [0,3]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> arr = [1,1,0,1,1]
<strong>Output:</strong> [-1,-1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> arr = [1,1,0,0,1]
<strong>Output:</strong> [0,2]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= arr.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>arr[i]</code> is <code>0</code> or <code>1</code></li>
</ul>

class Solution {
public:
    vector<int> threeEqualParts(vector<int>& arr) {
        vector<int> nextOnes(arr.size(),-1);

        bool allZeros = true;
        int nextOne = -1;
        int oneCount = 0;
        int lastOne = -1;
        for(int i=arr.size()-1; i >= 0; i--) {
            if(arr[i] == 1) {
                if(allZeros) {
                    lastOne = i;
                }
                allZeros = false;
                nextOne = i;
                oneCount++;
            }
            nextOnes[i] = nextOne;
        }

        if(allZeros)
            return {0,2};

        if(oneCount < 3)
            return {-1,-1};

        int n = arr.size();
        int start = nextOnes[0];


        for(int i=start; i < n-2; i++) {
            int j = i + nextOnes[i+1] - nextOnes[0] + 1;
            if(j >= n)
                continue;
            
            if(j + nextOnes[j] == n + nextOnes[i+1]) {

                bool equals = true;
                int midIndex = nextOnes[i+1];
                for(int k=nextOnes[0]; k <= i; k++) {
                    if(arr[k] != arr[midIndex+k-nextOnes[0]]) {
                        equals = false;
                        break;
                    } 
                }


                for(int k=nextOnes[0]; k <= i; k++) {
                    if(arr[k] != arr[nextOnes[j]+k-nextOnes[0]]) {
                        equals = false;
                        break;
                    } 
                }

                if(equals) {
                    return {i,j};
                }


            }
            
        }

        return {-1,-1};

        // while(i < j-1) {
        //     int left = i-nextOnes[0]+1;
        //     int right = arr.size() - nextOnes[j];

        //     int midIndex = nextOnes[i+1];
        //     if(midIndex >= j)
        //         return {-1,-1};
        
        //     int mid = j-midIndex+1;

        //     cout << i << " " << j << " " << left << " " << mid << " " << right  << endl;


        //     if(left == mid && mid == right) {

        //         bool equals = true;
        //         for(int k=nextOnes[0]; k <= i; k++) {
        //             if(arr[k] != arr[midIndex+k-nextOnes[0]]) {
        //                 equals = false;
        //                 break;
        //             } 
        //         }


        //         for(int k=nextOnes[0]; k <= i; k++) {
        //             if(arr[k] != arr[nextOnes[j]+k-nextOnes[0]]) {
        //                 equals = false;
        //                 break;
        //             } 
        //         }

        //         if(equals) {
        //             return {i,j};
        //         }

        //     }
        //     else if(left < mid && right < mid) {
        //         // i++;
        //         j--;
        //     }
        //     else if(left < mid && right == mid) {
        //         i++;
        //     }
        //     else if(left == mid && right < mid) {
        //         j--;
        //     }
        //     else {
        //         return {-1,-1};
        //     }

        // }

        return {-1,-1};
    }
};
43100