JuniorArrayMath

Minimum Operations to Make Array Sum Divisible by K

You are given an integer array nums and an integer k. You can perform the following operation any number of times:

  • Select an index i and replace nums[i] with nums[i] - 1.

Return the minimum number of operations required to make the sum of the array divisible by k.

Example 1
Inputnums = [3,9,7], k = 5
Output4
Perform 4 operations on nums[1] = 9 to make the array [3, 5, 7], whose sum is 15 and divisible by 5.
Example 2
Inputnums = [4,1,3], k = 4
Output0
The sum is 8, which is already divisible by 4, so no operations are needed.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • 1 <= k <= 100

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate