Dsa
Prefix Sum
Medium
Tri-Color Cargo Balance
A logistics center processes items with weights 1, 2, or 3. To maintain belt stability, an engineer wants to find the longest sequence of items where the number of items of weight 1, weight 2, and weight 3 are all perfectly equal.
Input Format:
The first line contains N, the number of items.
The second line contains N integers, each being 1, 2, or 3.
Output Format:
A single integer representing the length of the longest balanced subarray.
Example:
Input:
7
1 2 3 1 2 3 1
Output:
6
Explanation: The subarray [1, 2, 3, 1, 2, 3] has two 1s, two 2s, and two 3s.
Key concepts
prefix_sumhash_maptuples
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now