Simple Python Solution with explanation 🔥
Approach\n\n> divide the array into three non-empty parts such that all of these parts represent the same binary value\n\nSame binary value means \n\n each part
# Approach\n\n> *divide the array into three non-empty parts such that all of these parts represent the same binary value*\n\nSame binary value means \n\n* each part has to have **<ins>equal number of ones<ins>** and zeroes\n* order of ones and zeroes should be same\n\n\n### Part 1: Equal number of ones and ending zeroes\n\n\nLets take below array as an example for understanding:\n\n![Untitled-2023-01-11-1852(17).png](https://assets.leetcode.com/users/images/e38a4680-1a58-495f-b448-dd8ed52ef478_1673977794.7565248.png)\n\n\n* These binary values will have equal number of ones, which means the total count of ones should be a multiple of 3\n\n\n    ![Untitled-2023-01-11-1852(18).png](https://assets.leetcode.com/users/images/3deeed2e-67fb-4081-84b4-21022fe00271_1673977804.008318.png)\n\n\n\n* We know in a binary number, starting zeroes doesnt matter, however ending zeroes does matter. So I can find the `ending_zeroes` for my binary number using the location of last one:\n\n    ![Untitled-2023-01-11-1852(14).png](https://assets.leetcode.com/users/images/e0b8d6c8-fb23-4a46-94da-3f0e5f9a73c9_1673977354.461905.png)\n\n\n* We need to add these ending_zeroes count after every `total_count_of_ones // 3` ie in the example after every 3 count of ones\n\n    ![Untitled-2023-01-11-1852(16).png](https://assets.leetcode.com/users/images/cd4192ca-1d8e-47ed-a2ed-3ab48d0670f0_1673977459.8463302.png)\n\n* Now, we know the location of `i` and `j`\n\n\n\nNOTE:\n    \n* All above steps can be done by traversing through the loop, \n\n    however in my code below I have used prefix sum and binary search --> using modified binary_search to fetch index of the first occurrence in the prefix_sum \n\n    for example: \n    ```\n    ending_zeroes = n - 1 - (index of first occurrence of total_count_of_ones in prefix_sum, ie 9 in this example)\n    ```\n    \n\n    ![Untitled-2023-01-11-1852(15).png](https://assets.leetcode.com/users/images/50ef8629-2165-40b8-b3f4-303596ecb423_1673980808.0708501.png)\n\n\n    Creation of prefix sum takes `O(n)` time, so no major optimisation with this, this just looked clean to me :)\n\n\n\n### Part 2: Order of ones and zeroes\n\nThis is simple, for each identified part\n\n* disregard starting zeroes\n* then, check if order of each ones and zeroes is same\n\n\n\n\n\n\n# Complexity\n- Time complexity:\n$$O(n + log(n))$$ = $$O(n)$$\n\n- Space complexity:\n$$O(n)$$ - for prefix_sum\n\n# Code\n\n```python []\nclass Solution:\n    def threeEqualParts(self, arr: List[int]) -> List[int]:\n        n = len(arr)\n\n        # part 1\n\n        # get total_count and create prefix_sum\n        total_count = 0\n        prefix_sum = [0] * n\n        for i in range(n):\n            total_count += arr[i]\n            prefix_sum[i] = total_count\n\n        # checking if multiple of 3\n        if total_count % 3 != 0:\n            return [-1, -1]\n        elif total_count == 0:\n            return [0, n - 1]\n\n        # binary search to get first occurrence of `val` using prefix_sum\n        def bin_search(val):\n            start = 0\n            end = n - 1\n            mid = 0\n\n            while start <= end:\n                mid = (start + end) // 2\n                if prefix_sum[mid] >= val:\n                    if start == end:\n                        return mid\n                    end = mid\n                else:\n                    start = mid + 1\n\n            return mid\n\n        \n        # count of ones in each part and count of ending_zeroes\n        part_count = total_count // 3\n        ending_zeroes = n - 1 - bin_search(total_count)\n\n        # value of i and j using binary_search and ending_zeroes\n        i = bin_search(part_count) + ending_zeroes + 1\n        j = bin_search(total_count - part_count) + ending_zeroes + 1\n\n\n\n        # part 2\n\n        # disregard starting zeroes in first part\n        a = 0\n        while a < n and arr[a] == 0:\n            a += 1\n\n        # disregard starting zeroes in second part\n        b = i\n        while b < n and arr[b] == 0:\n            b += 1\n\n        # disregard starting zeroes in third part\n        c = j\n        while c < n and arr[c] == 0:\n            c += 1\n\n        # check if indices have same order of ones and zeroes\n        while c < n:\n            if arr[a] == arr[b] and arr[b] == arr[c]:\n                a += 1\n                b += 1\n                c += 1\n            else:\n                return [-1, -1]\n\n        if a == i and b == j:\n            return [i - 1, j]\n        else:\n            return [-1, -1]\n\n```\n
['Binary Search', 'Prefix Sum', 'Python', 'Python3']