g0901_1000.s0927_three_equal_parts.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0901_1000.s0927_three_equal_parts;
// #Hard #Array #Math #2022_03_29_Time_2_ms_(90.20%)_Space_47.4_MB_(96.08%)
/**
* 927 - Three Equal Parts\.
*
* Hard
*
* 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, and
* * `arr[j], arr[j + 1], ..., arr[arr.length - 1]` is the third part.
* * All three parts have equal binary values.
*
* 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 * 104
* * `arr[i]` is `0` or `1`
**/
public class Solution {
public int[] threeEqualParts(int[] arr) {
int ones = 0;
for (int num : arr) {
ones += num;
}
if (ones == 0) {
return new int[] {0, 2};
} else if (ones % 3 != 0) {
return new int[] {-1, -1};
}
ones /= 3;
int index1 = -1;
int index2 = -1;
int index3 = -1;
int totalOnes = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
continue;
}
totalOnes += arr[i];
if (totalOnes == 1) {
index1 = i;
} else if (totalOnes == ones + 1) {
index2 = i;
} else if (totalOnes == 2 * ones + 1) {
index3 = i;
}
}
while (index3 < arr.length) {
if (arr[index1] == arr[index3] && arr[index2] == arr[index3]) {
++index1;
++index2;
++index3;
} else {
return new int[] {-1, -1};
}
}
return new int[] {index1 - 1, index2};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy