Given an array A of length n such that it contains only ‘0s’ and ‘1s’. The task is to divide the array into THREE different non-empty parts such that all of these parts represent the same binary value(in decimals).
If it is possible, return any [i, j] with i+1 < j, such that:
1. A, A, …, A[i] is the first part.
2. A[i+1], A[i+2], …, A[j-1] is the second part.
3. A[j], A[j+1], …, A[n- 1] is the third part.
Note: All three parts should have equal binary value. However, If it is not possible, return [-1, -1].
Input : A = [1, 1, 1, 1, 1, 1] Output : [1, 4] All three parts are, A to A first part, A to A second part, A to A third part. Input : A = [1, 0, 0, 1, 0, 1] Output : [0, 4]
Lets say total number of ones in A be S. Since every part has the same number of ones, thus all parts should have K = S / 3 ones.
If S isn’t divisible by 3 i:e S % 3 != 0, then the task is impossible.
Now we will find the position of the 1st, K-th, K+1-th, 2K-th, 2K+1-th, and 3K-th one. The positions of these ones will form 3 intervals: [i1, j1], [i2, j2], [i3, j3]. (If there are only 3 ones, then the intervals are each length 1.)
Between the intervals, there may be some number of zeros. The zeros after third interval j3 must be included in each part: say there are z of them (z = length of (S) – j3).
So the first part, [i1, j1], is now [i1, j1+z]. Similarly, the second part, [i2, j2], is now [i2, j2+z].
If all this is actually possible, then the final answer is [j1+z, j2+z+1].
Below is the implementation of above approach.
Time Complexity: O(N), where N is the length of S.
Space Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Split the array into equal sum parts according to given conditions
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Divide array into two sub-arrays such that their averages are equal
- Number of ways to divide an array into K equal sum sub-arrays
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Divide array in two maximum equal length arrays of similar and dissimilar elements
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Count ways to partition a string such that both parts have equal distinct characters
- Optimal partition of an array into four parts
- Divide every element of one array by other array elements
- Divide an array into K subarray with the given condition
- Divide the array in K segments such that the sum of minimums is maximized
- Online Queries for GCD of array after divide operations
- Frequency of an integer in the given array using Divide and Conquer
- Count number of ways to divide an array into two halves with same sum
- Find all numbers that divide maximum array elements
- Divide the array into minimum number of sub-arrays having unique elements
- Divide an array into k segments to maximize maximum of segment minimums
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Harshit Saini