Talentd
FleetCode

3190. Find Minimum Operations to Make All Elements Divisible by Three

Easy90.9% acceptance3 companies

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:

Input: nums = [1,2,3,4]

Output: 3

Explanation:

All array elements can be made divisible by 3 using 3 operations:

  • Subtract 1 from 1.
  • Add 1 to 2.
  • Subtract 1 from 4.

Example 2:

Input: nums = [3,6,9]

Output: 0

Constraints:

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

Asked at 3 companies

Topics

Hints open one at a time

Hint 1
If x % 3 != 0 we can always increment or decrement x such that we only need 1 operation.
Hint 2
Add min(nums[i] % 3, 3 - (num[i] % 3)) to the count of operations.
5:48
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minimumOperations(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3,4]
Input
[3,6,9]
Your code is saved on this device.Solution