3937. Minimum Operations to Make Array Modulo Alternating I
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
ArrayEnumeration
Hints
Hint 1
Bruteforce for all combinations of
x and y, making sure they are distinct.Hint 2
For each element, we either increase it until the condition is satisfied, or decrease.
Check both options and pick the one that uses a smaller number of operations.
Preparing judge…