Balanced Mod Subarrays
In Balanced Mod Subarrays, you work with an integer list and split each number by its remainder modulo 3, so every value belongs to class 0, 1, or 2. Your goal is to return contiguous segments where those three classes appear in equal counts. The important part is that the task asks for inclusion-maximal balanced segments, meaning each returned segment cannot be extended left or right and still stay balanced.
A practical way to think about this is counting remainder frequencies inside candidate windows, then validating balance and maximality before adding them to output. Negative numbers still need correct modulo handling, so your implementation should normalize remainder calculation consistently across languages.
The judge checks both simple and dense inputs, including empty arrays and data where many overlapping balanced windows exist. Output order matters: keep segments in the same order they appear in the original sequence. Your return value should contain only valid maximal balanced subarrays, with no duplicates and no smaller subsegments that are fully contained inside a larger balanced segment.
Because this is a counting/optimization-style challenge, dynamic programming is usually the safest approach: define what each index or state means, initialize valid base states, and make transitions explicit. If the problem uses modulo arithmetic, apply modulo at every accumulation step so large intermediate totals never corrupt the final answer.
Examples
The entire array has two values from each remainder class and is maximal.
Two maximal balanced subarrays cover the timeline; neither can be extended without breaking the balance.
All values share the same remainder, so no balanced subarray exists.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public int[][] find_maximal_balanced_subarrays(int[] nums) {
int p0 = 0, p1 = 0, p2 = 0;
Map<String, List<Integer>> diffs = new HashMap<>();
diffs.computeIfAbsent("0,0", k -> new ArrayList<>()).add(-1);
for (int i = 0; i < nums.length; i++) {
int r = (nums[i] % 3 + 3) % 3;
if (r == 0) p0++;
else if (r == 1) p1++;
else p2++;
String key = (p0 - p1) + "," + (p1 - p2);
diffs.computeIfAbsent(key, k -> new ArrayList<>()).add(i);
}
List<int[]> balanced = new ArrayList<>();
for (List<Integer> indices : diffs.values()) {
for (int a = 0; a < indices.size(); a++) {
for (int b = a + 1; b < indices.size(); b++) {
balanced.add(new int[]{indices.get(a) + 1, indices.get(b)});
}
}
}
List<int[]> maximal = new ArrayList<>();
for (int[] b1 : balanced) {
boolean isSub = false;
for (int[] b2 : balanced) {
if (b2[0] <= b1[0] && b2[1] >= b1[1] && (b2[0] != b1[0] || b2[1] != b1[1])) {
isSub = true;
break;
}
}
if (!isSub) maximal.add(b1);
}
maximal.sort(Comparator.comparingInt(a -> a[0]));
int[][] result = new int[maximal.size()][];
for (int i = 0; i < maximal.size(); i++) {
int start = maximal.get(i)[0];
int end = maximal.get(i)[1];
result[i] = Arrays.copyOfRange(nums, start, end + 1);
}
return result;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
