Welcome to Subscribe On Youtube
3937. Minimum Operations to Make Array Modulo Alternating I
Description
You are given an integer array nums and an integer k.
In one operation, you can increase or decrease any element of nums by 1.
An array is called modulo alternating if there exist two distinct integers x and y (0 <= x, y < k) such that:
- For every even index
i,nums[i] % k == x - For every odd index
i,nums[i] % k == y
Return the minimum number of operations required to make nums modulo alternating.
Example 1:
Input: nums = [1,4,2,8], k = 3
Output: 2
Explanation:
- Let's choose
x = 1for even indices andy = 2for odd indices. - Perform the following operations:
- Increment
nums[1] = 4by 1, givingnums = [1, 5, 2, 8]. - Decrement
nums[2] = 2by 1, givingnums = [1, 5, 1, 8].
- Increment
- Now, for even indices,
nums[i] % k = 1, and for odd indices,nums[i] % k = 2. - Thus, the total number of operations required is 2.
Example 2:
Input: nums = [1,1,1], k = 3
Output: 1
Explanation:
- Incrementing
nums[1]by 1 givesnums = [1, 2, 1], which satisfies the condition withx = 1andy = 2. - Thus, the total number of operations required is 1.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1092 <= k <= 100
Solutions
Solution 1: Enumeration
We can enumerate the target value $x$ for even indices and the target value $y$ for odd indices, where $0 \leq x, y < k$ and $x \neq y$. For each element, we calculate the number of operations required to change it to the target value, and accumulate the total number of operations. Finally, we return the minimum value among all enumeration results.
The time complexity is $O(n \times k^2)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
-
class Solution { public int minOperations(int[] nums, int k) { int n = nums.length; for (int i = 0; i < n; ++i) { nums[i] %= k; } int ans = Integer.MAX_VALUE; for (int x = 0; x < k; ++x) { for (int y = 0; y < k; ++y) { if (x != y) { int cnt = 0; for (int i = 0; i < n; ++i) { int target = (i & 1) == 0 ? x : y; int diff = Math.abs(target - nums[i]); cnt += Math.min(diff, k - diff); } ans = Math.min(ans, cnt); } } } return ans; } } -
class Solution { public: int minOperations(vector<int>& nums, int k) { int n = nums.size(); for (int& v : nums) { v %= k; } int ans = INT_MAX; for (int x = 0; x < k; ++x) { for (int y = 0; y < k; ++y) { if (x != y) { int cnt = 0; for (int i = 0; i < n; ++i) { int target = (i & 1) ? y : x; int diff = abs(target - nums[i]); cnt += min(diff, k - diff); } ans = min(ans, cnt); } } } return ans; } }; -
class Solution: def minOperations(self, nums: list[int], k: int) -> int: for i, v in enumerate(nums): nums[i] = v % k ans = inf for x in range(k): for y in range(k): if x != y: cnt = 0 for i, v in enumerate(nums): target = x if i % 2 == 0 else y diff = abs(target - v) cnt += min(diff, k - diff) ans = min(ans, cnt) return ans -
func minOperations(nums []int, k int) int { for i, v := range nums { nums[i] = v % k } ans := int(^uint(0) >> 1) for x := 0; x < k; x++ { for y := 0; y < k; y++ { if x != y { cnt := 0 for i, v := range nums { target := x if i&1 == 1 { target = y } diff := abs(target - v) cnt += min(diff, k-diff) } ans = min(ans, cnt) } } } return ans } func abs(x int) int { if x < 0 { return -x } return x } -
function minOperations(nums: number[], k: number): number { const n = nums.length; for (let i = 0; i < n; ++i) { nums[i] %= k; } let ans = Infinity; for (let x = 0; x < k; ++x) { for (let y = 0; y < k; ++y) { if (x !== y) { let cnt = 0; for (let i = 0; i < n; ++i) { const target = (i & 1) === 0 ? x : y; const diff = Math.abs(target - nums[i]); cnt += Math.min(diff, k - diff); } ans = Math.min(ans, cnt); } } } return ans; }