963. Three Equal Parts
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Three-Pointer Comparison based on Ones Distribution
The core of the problem is to find two cut points, i and j, that divide an array of binary digits into three parts with equal binary values. A key piece of information is that leading zeros do not affect the binary value of a number (e.g., [0, 1, 1] and [1, 1] both represent 3). This implies that the value of each part is determined by the pattern of ones and zeros starting from its first '1'.
Solution
Three Equal Parts in Python
class Solution:
def threeEqualParts(self, arr: List[int]) -> List[int]:
n = len(arr)
one_indices = [i for i, x in enumerate(arr) if x == 1]
total_ones = len(one_indices)
# Case 1: No ones, any partition is valid.
# The problem requires i + 1 < j, and non-empty parts.
# For n >= 3, [0, 2] works: [0], [0], [0...]
# The example output for [0,0,0] is [0,2]. We return a more general one for simplicity.
if total_ones == 0:
return [0, n - 1]
# Case 2: Number of ones is not divisible by 3.
if total_ones % 3 != 0:
return [-1, -1]
k = total_ones // 3
# Identify the start of the three blocks of ones.
start1 = one_indices[0]
start2 = one_indices[k]
start3 = one_indices[2 * k]
# The pattern is determined by the third part's block.
len_suffix = n - start3
# Compare the three blocks.
if not (arr[start1 : start1 + len_suffix] == arr[start2 : start2 + len_suffix] == arr[start3 : start3 + len_suffix]):
return [-1, -1]
# If patterns match, find the partition points.
i = start1 + len_suffix - 1
j = start2 + len_suffix
# The condition i + 1 < j is guaranteed because start2 > start1.
return [i, j]
Three Equal Parts in Java
class Solution {
public int[] threeEqualParts(int[] arr) {
int n = arr.length;
ArrayList<Integer> oneIndices = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (arr[i] == 1) {
oneIndices.add(i);
}
}
int totalOnes = oneIndices.size();
// Case 1: No ones, any partition is valid.
if (totalOnes == 0) {
return new int[]{0, n - 1}; // or {0, 2} since n>=3
}
// Case 2: Number of ones is not divisible by 3.
if (totalOnes % 3 != 0) {
return new int[]{-1, -1};
}
int k = totalOnes / 3;
// Identify the start of the three blocks of ones.
int start1 = oneIndices.get(0);
int start2 = oneIndices.get(k);
int start3 = oneIndices.get(2 * k);
// The pattern is determined by the third part's block.
int lenSuffix = n - start3;
// Compare the three blocks.
int p1 = start1, p2 = start2, p3 = start3;
while (p3 < n) {
if (arr[p1] != arr[p2] || arr[p2] != arr[p3]) {
return new int[]{-1, -1};
}
p1++;
p2++;
p3++;
}
// If patterns match, find the partition points.
// The condition i + 1 < j is guaranteed by start2 > start1
int i = start1 + lenSuffix - 1;
int j = start2 + lenSuffix;
return new int[]{i, j};
}
}
Three Equal Parts in C++
class Solution {
public:
vector<int> threeEqualParts(vector<int>& arr) {
int n = arr.size();
vector<int> one_indices;
for (int i = 0; i < n; ++i) {
if (arr[i] == 1) {
one_indices.push_back(i);
}
}
int total_ones = one_indices.size();
// Case 1: No ones, any partition is valid.
if (total_ones == 0) {
return {0, n - 1}; // Or {0, 2} since n>=3
}
// Case 2: Number of ones is not divisible by 3.
if (total_ones % 3 != 0) {
return {-1, -1};
}
int k = total_ones / 3;
// Identify the start of the three blocks of ones.
int start1 = one_indices[0];
int start2 = one_indices[k];
int start3 = one_indices[2 * k];
// The pattern is determined by the third part's block.
int len_suffix = n - start3;
// Compare the three blocks.
int p1 = start1, p2 = start2, p3 = start3;
while (p3 < n) {
if (arr[p1] != arr[p2] || arr[p2] != arr[p3]) {
return {-1, -1};
}
p1++;
p2++;
p3++;
}
// If patterns match, find the partition points.
int i = start1 + len_suffix - 1;
int j = start2 + len_suffix;
return {i, j};
}
};
Three Equal Parts in TypeScript
function threeEqualParts(arr: number[]): number[] {
const n = arr.length;
const oneIndices: number[] = [];
for (let i = 0; i < n; i++) {
if (arr[i] === 1) {
oneIndices.push(i);
}
}
const totalOnes = oneIndices.length;
// Case 1: No ones, any partition is valid.
if (totalOnes === 0) {
return [0, n - 1]; // or [0, 2] since n >= 3
}
// Case 2: Number of ones is not divisible by 3.
if (totalOnes % 3 !== 0) {
return [-1, -1];
}
const k = totalOnes / 3;
// Identify the start of the three blocks of ones.
const start1 = oneIndices[0];
const start2 = oneIndices[k];
const start3 = oneIndices[2 * k];
// The pattern is determined by the third part's block.
const lenSuffix = n - start3;
// Compare the three blocks.
let p1 = start1, p2 = start2, p3 = start3;
while (p3 < n) {
if (arr[p1] !== arr[p2] || arr[p2] !== arr[p3]) {
return [-1, -1];
}
p1++;
p2++;
p3++;
}
// If patterns match, find the partition points.
// The condition i + 1 < j is guaranteed by start2 > start1.
const i = start1 + lenSuffix - 1;
const j = start2 + lenSuffix;
return [i, j];
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.