Minimum Operations to Make Array Sum Divisible by K - Problem
You are given an integer array nums and an integer k.
You can perform the following operation any number of times:
- Select an index
iand replacenums[i]withnums[i] - 1
Return the minimum number of operations required to make the sum of the array divisible by k.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,1,4,2], k = 6
›
Output:
4
💡 Note:
Sum = 3+1+4+2 = 10. Since 10 % 6 = 4, we need 4 operations to reduce the sum by 4 and make it divisible by 6.
Example 2 — Already Divisible
$
Input:
nums = [2,4], k = 3
›
Output:
0
💡 Note:
Sum = 2+4 = 6. Since 6 % 3 = 0, the sum is already divisible by 3, so 0 operations needed.
Example 3 — Single Element
$
Input:
nums = [5], k = 3
›
Output:
2
💡 Note:
Sum = 5. Since 5 % 3 = 2, we need 2 operations to reduce 5 to 3, making it divisible by 3.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
- 1 ≤ k ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code