JuniorArrayMath

Find Minimum Operations to Make All Elements Divisible by Three

You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums.

Return the minimum number of operations to make all elements of nums divisible by 3.

Example 1
Inputnums = [1,2,3,4]
Output3
All array elements can be made divisible by 3 using 3 operations: subtract 1 from 1, add 1 to 2, and subtract 1 from 4.
Example 2
Inputnums = [3,6,9]
Output0
All elements are already divisible by 3, so no operations are needed.

Constraints

  • 1 <= nums.length <= 50
  • 1 <= nums[i] <= 50

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