<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:
    def threeEqualParts(self, arr: List[int]) -> List[int]:
        freq1=arr.count(1)
        if freq1%3!=0:
            return [-1,-1]
        count= freq1//3
        if count==0:
            return [0,2]
        temp=0
        trailing_flag=True
        trailing_zero=0
        trailing_zero_index= len(arr)
        for j in range(len(arr)-1,-1,-1):
            if arr[j]==1:
                temp+=1
                if trailing_flag:
                    trailing_zero_index=j+1
                    trailing_flag=False
            elif arr[j]==0 and trailing_flag:
                trailing_zero+=1

            if temp==count:
                break
        ### can potentially have leading zeroes
        target= arr[j:trailing_zero_index]

        ##search the first part, i.e. left most part
        temp=0
        leading_zero_index=0
        leading_flag=True
        for i in range(j):
            if arr[i]==1:
                temp+=1
                if leading_flag:
                    leading_flag=False
                    lead_zero_index=i
                if temp==count:
                    break
        if target!= arr[lead_zero_index: i+1]:
            return [-1,-1]
        for index in range(i+1, i+ trailing_zero+1):
            if arr[index]==1:
                return [-1,-1]

        res1= i+trailing_zero
        ###search the middle part

        temp=0
        start_index= i+trailing_zero+1
        start_flag=True
        for k in range(i+trailing_zero+1, j):
            if arr[k]==1:
                if start_flag:
                    start_index= k
                    start_flag=False
                temp+=1
            if temp==count:
                if target!= arr[start_index: k+1]:
                    return [-1,-1]
                if (k+trailing_zero)<j:
                    return [res1, k+trailing_zero+1]
                else:
                    return [-1,-1]


17400