[LeetCode] Find Minimum Operations to Make All Elements Divisible by Three

3190. 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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int minimumOperations(vector<int>& nums) {
int res = 0;
for(auto& x : nums) {
if(x % 3) res++;
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/23/PS/LeetCode/find-minimum-operations-to-make-all-elements-divisible-by-three/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.