A neighborhood bakery packages its dinner rolls onto n trays before boxing them for delivery, and shop policy requires every tray to hold a number of rolls that is a multiple of 3 before it can be boxed. You are given the current roll count on each of the n trays. In one operation, the baker may pick any single tray and either add one roll to it or remove one roll from it. Determine the minimum total number of operations required so that every tray ends up holding a number of rolls divisible by 3.
The first line contains a single integer n (1 <= n <= 50) — the number of trays.
The second line contains n space-separated integers nums[0], nums[1], ..., nums[n-1] (0 <= nums[i] <= 50), the current roll count on each tray.
A single integer: the minimum total number of operations needed so that every tray's count is a multiple of 3.
Example 1
Input
3 1 2 3
Expected
2
Explanation
Tray 0 has 1 roll (needs 1 removal to reach 0), tray 1 has 2 rolls (needs 1 addition to reach 3), tray 2 has 3 rolls, already divisible by 3. Total operations = 1 + 1 + 0 = 2.
Example 2
Input
4 0 3 6 9
Expected
0
Explanation
All four tray counts (0, 3, 6, 9) are already multiples of 3, so no operations are needed; the answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →