2025-11-22 Daily Challenge
Today I have done leetcode's November LeetCoding Challenge with cpp.
November LeetCoding Challenge 22
Description
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:
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 <= 501 <= nums[i] <= 50
Solution
class Solution {
public:
int minimumOperations(vector<int>& nums) {
int answer = 0;
for(auto n : nums) {
answer += !!(n % 3);
}
return answer;
}
};
// Accepted
// 660/660 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 10.78 % of cpp submissions (23.4 MB)