You are given an array arr which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value.
If it is possible, return any [i, j] with i + 1 < j, such that:
arr[0], arr[1], ..., arr[i] is the first part,arr[i + 1], arr[i + 2], ..., arr[j - 1] is the second part, andarr[j], arr[j + 1], ..., arr[arr.length - 1] is the third part.If it is not possible, return [-1, -1].
Note that the entire part is used when considering what binary value it represents. For example, [1,1,0] represents 6 in decimal, not 3. Also, leading zeros are allowed, so [0,1,1] and [1,1] represent the same value.
Example 1:
Input: arr = [1,0,1,0,1] Output: [0,3]
Example 2:
Input: arr = [1,1,0,1,1] Output: [-1,-1]
Example 3:
Input: arr = [1,1,0,0,1] Output: [0,2]
Constraints:
3 <= arr.length <= 3 * 104arr[i] is 0 or 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long sequence of binary digits and you want to slice it into three consecutive chunks so that the numerical value represented by each chunk is identical. The brute force strategy works by exhaustively testing every possible pair of split points to divide the sequence into three parts and then comparing their values.
Here's how the algorithm would work step-by-step:
def three_equal_parts(binary_array):
# We test every possible pair of split points to ensure we evaluate all partitioning combinations.
for first_cut_index in range(1, len(binary_array) - 1):
for second_cut_index in range(first_cut_index + 1, len(binary_array)):
first_chunk = binary_array[:first_cut_index]
middle_chunk = binary_array[first_cut_index:second_cut_index]
final_chunk = binary_array[second_cut_index:]
# We compare the numeric values of the slices after removing leading zeros for accuracy.
if int("".join(map(str, first_chunk)), 2) == int("".join(map(str, middle_chunk)), 2) == int("".join(map(str, final_chunk)), 2):
return [first_cut_index - 1, second_cut_index]
# If all partition combinations fail, we return negative indices to indicate it is impossible.
return [-1, -1]
The optimal strategy breaks the binary sequence into three equally weighted numerical values by counting the total number of ones and dividing them into three identical groups. It then uses the positions of these specific ones to pinpoint the exact boundaries where the sequence must be cut.
Here's how the algorithm would work step-by-step:
def three_equal_parts(binary_array):
total_ones_count = sum(binary_array)
# If the total ones cannot be split into three equal groups, an equal division is impossible.
if total_ones_count % 3 != 0:
return [-1, -1]
if total_ones_count == 0:
return [0, len(binary_array) - 1]
ones_per_group = total_ones_count // 3
current_ones_seen = 0
first_group_start = second_group_start = third_group_start = 0
for index, bit_value in enumerate(binary_array):
if bit_value == 1:
current_ones_seen += 1
if current_ones_seen == 1:
first_group_start = index
elif current_ones_seen == ones_per_group + 1:
second_group_start = index
elif current_ones_seen == (2 * ones_per_group) + 1:
third_group_start = index
trailing_zeros_count = len(binary_array) - third_group_start
# Ensure there are enough trailing zeros to match the spacing requirements for all three sections.
if (first_group_start + trailing_zeros_count <= second_group_start) and \
(second_group_start + trailing_zeros_count <= third_group_start):
cursor_one = first_group_start
cursor_two = second_group_start
cursor_three = third_group_start
while cursor_three < len(binary_array):
# Verify that the digits in all three candidate sections match identically from this point.
if binary_array[cursor_one] == binary_array[cursor_two] == binary_array[cursor_three]:
cursor_one += 1
cursor_two += 1
cursor_three += 1
else:
return [-1, -1]
return [cursor_one - 1, cursor_two]
return [-1, -1]
| Case | How to Handle |
|---|---|
| Array length less than 3 | Return [-1, -1] immediately because it is impossible to divide the array into three non-empty parts. |
| Array contains all zeros | Count total ones as zero, and split the array at fixed indices around thirds of the length. |
| Total count of ones is not divisible by 3 | Return [-1, -1] because an equal binary value requires an equal number of significant ones in each part. |
| Leading or trailing zeros create overlapping part boundaries | Align the partition indices based on the exact positions of the first ones in each third block. |
| Maximum length constraints up to thirty thousand elements | Use a linear time single pass or constant pass approach to avoid time limit exceeded errors. |
| Trailing zeros after the last partition affecting value equality | Compare the actual binary digits of the three slices from their starting ones to the end of the third part. |
| Multiple valid split points due to trailing zeros | Greedily consume all trailing zeros of the third part by shifting the right partition boundary as far right as possible. |
| Array contains only a single one or too few ones to form three parts | The modulo check for the total count of ones will catch this and immediately return [-1, -1]. |