Given an array, find the minimum number of element changes needed so every value becomes divisible by 3.
You are given an integer array nums. In one operation, you may choose an element and change it by +1 or -1.
Return the minimum number of operations required so that every element in the array becomes divisible by 3.
Because each operation changes a value by exactly one, the cost to make a number divisible by 3 depends only on its remainder when divided by 3.
+1/-1 steps across the entire array.nums.1 <= nums.length <= $10^{5}$0 <= nums[i] <= $10^{9}$3 need no changes.1 can be fixed in one step.2 can also be fixed in one step by decreasing it by 1, or in two steps by increasing it to the next multiple of 3 depending on the allowed move strategy; choose the minimum per element.nums: integer array+1/-1 operations required so that all elements are divisible by 3.1 <= nums.length <= $10^{5}$0 <= nums[i] <= $10^{9}$Example 1
Input
nums = [3, 6, 1, 2]
Output
2
Explanation
3 and 6 are already divisible by 3. 1 needs one -1 operation, and 2 needs one -1 operation. Total = 2.
Example 2
Input
nums = [4, 7, 10]
Output
3
Explanation
Each value leaves remainder 1 when divided by 3, so each needs one operation to become divisible by 3. Total = 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.