3854. Minimum Operations to Make Array Parity Alternating Medium
1/**
2 * [3854] Minimum Operations to Make Array Parity Alternating
3 *
4 * You are given an integer array nums.
5 * An array is called parity alternating if for every index i where 0 <= i < n - 1, nums[i] and nums[i + 1] have different parity (one is even and the other is odd).
6 * In one operation, you may choose any index i and either increase nums[i] by 1 or decrease nums[i] by 1.
7 * Return an integer array answer of length 2 where:
8 *
9 * answer[0] is the minimum number of operations required to make the array parity alternating.
10 * answer[1] is the minimum possible value of max(nums) - min(nums) taken over all arrays that are parity alternating and can be obtained by performing exactly answer[0] operations.
11 *
12 * An array of length 1 is considered parity alternating.
13 *
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [-2,-3,1,4]</span>
17 * Output: <span class="example-io">[2,6]</span>
18 * Explanation:
19 * Applying the following operations:
20 *
21 * Increase nums[2] by 1, resulting in nums = [-2, -3, 2, 4].
22 * Decrease nums[3] by 1, resulting in nums = [-2, -3, 2, 3].
23 *
24 * The resulting array is parity alternating, and the value of max(nums) - min(nums) = 3 - (-3) = 6 is the minimum possible among all parity alternating arrays obtainable using exactly 2 operations.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">nums = [0,2,-2]</span>
29 * Output: <span class="example-io">[1,3]</span>
30 * Explanation:
31 * Applying the following operation:
32 *
33 * Decrease nums[1] by 1, resulting in nums = [0, 1, -2].
34 *
35 * The resulting array is parity alternating, and the value of max(nums) - min(nums) = 1 - (-2) = 3 is the minimum possible among all parity alternating arrays obtainable using exactly 1 operation.
36 * </div>
37 * <strong class="example">Example 3:
38 * <div class="example-block">
39 * Input: <span class="example-io">nums = [7]</span>
40 * Output: <span class="example-io">[0,0]</span>
41 * Explanation:
42 * No operations are required. The array is already parity alternating, and the value of max(nums) - min(nums) = 7 - 7 = 0, which is the minimum possible.
43 * </div>
44 *
45 * Constraints:
46 *
47 * 1 <= nums.length <= 10^5
48 * -10^9 <= nums[i] <= 10^9
49 *
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/minimum-operations-to-make-array-parity-alternating/
54// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-parity-alternating/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59 pub fn make_parity_alternating(nums: Vec<i32>) -> Vec<i32> {
60 vec![]
61 }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_3854() {
72 }
73}
74Back
© 2026 bowen.ge All Rights Reserved.