leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 7c560aefd7e6297696da49b1367787f444b500bf
parent c7685ca0695a67b16f435de8fb4589bd258be2fb
Author: Jack Sangdahl <jack@pngu.org>
Date:   Fri, 21 Nov 2025 21:01:12 -0700

3190. find the minimum operation to make all elements divisble by three

Diffstat:
Aminimum_operations_make_elem_div_three.c | 24++++++++++++++++++++++++
Aminimum_operations_make_elem_div_three.py | 22++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/minimum_operations_make_elem_div_three.c b/minimum_operations_make_elem_div_three.c @@ -0,0 +1,24 @@ +// 3190. Find Minimum Operations to Make All Elements Divisible by Three +#include "leetcode.h" + +/* + * you are given an ineger 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 + */ + +int minimumOperations(int *nums, int numsSize) { + // mask where numbers not divisble by 3 have bit set + const unsigned long long mask = 0x6DB6DB6DB6DB6DB6ULL; + int ans = 0; + for (int i = 0; i < numsSize; i++) + if ((mask >> nums[i]) & 1) + ans++; + return ans; +} + +int main() { + int n1[] = {1, 2, 3, 4}, n2[] = {3, 6, 9}; + printf("%d\n", minimumOperations(n1, ARRAY_SIZE(n1))); // expect: 3 + printf("%d\n", minimumOperations(n2, ARRAY_SIZE(n2))); // expect: 0 +} diff --git a/minimum_operations_make_elem_div_three.py b/minimum_operations_make_elem_div_three.py @@ -0,0 +1,22 @@ +# 3190. Find Minimum Operations to Make All Elements Divisible by Three + +""" +you are given an ineger 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 +""" + + +class Solution(object): + def minimumOperations(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return sum(i % 3 > 0 for i in nums) + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumOperations(nums=[1, 2, 3, 4])) + print(obj.minimumOperations(nums=[3, 6, 9]))