diff --git a/README.md b/README.md index ca4318ab..baec6d02 100644 --- a/README.md +++ b/README.md @@ -2674,6 +2674,7 @@ 3183|[The Number of Ways to Make the Sum](./solutions/3183-the-number-of-ways-to-make-the-sum.js)|Medium| 3186|[Maximum Total Damage With Spell Casting](./solutions/3186-maximum-total-damage-with-spell-casting.js)|Medium| 3189|[Minimum Moves to Get a Peaceful Board](./solutions/3189-minimum-moves-to-get-a-peaceful-board.js)|Medium| +3190|[Find Minimum Operations to Make All Elements Divisible by Three](./solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js)|Easy| 3191|[Minimum Operations to Make Binary Array Elements Equal to One I](./solutions/3191-minimum-operations-to-make-binary-array-elements-equal-to-one-i.js)|Medium| 3195|[Find the Minimum Area to Cover All Ones I](./solutions/3195-find-the-minimum-area-to-cover-all-ones-i.js)|Medium| 3197|[Find the Minimum Area to Cover All Ones II](./solutions/3197-find-the-minimum-area-to-cover-all-ones-ii.js)|Hard| @@ -2777,6 +2778,7 @@ 3506|[Find Time Required to Eliminate Bacterial Strains](./solutions/3506-find-time-required-to-eliminate-bacterial-strains.js)|Hard| 3508|[Implement Router](./solutions/3508-implement-router.js)|Medium| 3511|[Make a Positive Array](./solutions/3511-make-a-positive-array.js)|Medium| +3512|[Minimum Operations to Make Array Sum Divisible by K](./solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js)|Easy| 3516|[Find Closest Person](./solutions/3516-find-closest-person.js)|Easy| 3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium| 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| diff --git a/solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js b/solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js new file mode 100644 index 00000000..24effff2 --- /dev/null +++ b/solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js @@ -0,0 +1,21 @@ +/** + * 3190. Find Minimum Operations to Make All Elements Divisible by Three + * https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/ + * Difficulty: Easy + * + * 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. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var minimumOperations = function(nums) { + return nums.reduce((operations, num) => { + const remainder = num % 3; + return operations + Math.min(remainder, 3 - remainder); + }, 0); +}; diff --git a/solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js b/solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js new file mode 100644 index 00000000..772bb4dc --- /dev/null +++ b/solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js @@ -0,0 +1,21 @@ +/** + * 3512. Minimum Operations to Make Array Sum Divisible by K + * https://leetcode.com/problems/minimum-operations-to-make-array-sum-divisible-by-k/ + * Difficulty: Easy + * + * You are given an integer array nums and an integer k. You can perform the following + * operation any number of times: + * - Select an index i and replace nums[i] with nums[i] - 1. + * + * Return the minimum number of operations required to make the sum of the array + * divisible by k. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var minOperations = function(nums, k) { + return nums.reduce((sum, n) => sum + n, 0) % k; +};