1674. Minimum Moves to Make Array Complementary Medium

@problem@discussion
#Array#Hash Table#Prefix Sum



1/**
2 * [1674] Minimum Moves to Make Array Complementary
3 *
4 * You are given an integer array nums of even length n and an integer limit. In one move, you can replace any integer from nums with another integer between 1 and limit, inclusive.
5 * The array nums is complementary if for all indices i (0-indexed), nums[i] + nums[n - 1 - i] equals the same number. For example, the array [1,2,3,4] is complementary because for all indices i, nums[i] + nums[n - 1 - i] = 5.
6 * Return the minimum number of moves required to make nums complementary.
7 *  
8 * Example 1:
9 * 
10 * Input: nums = [1,2,4,3], limit = 4
11 * Output: 1
12 * Explanation: In 1 move, you can change nums to [1,2,<u>2</u>,3] (underlined elements are changed).
13 * nums[0] + nums[3] = 1 + 3 = 4.
14 * nums[1] + nums[2] = 2 + 2 = 4.
15 * nums[2] + nums[1] = 2 + 2 = 4.
16 * nums[3] + nums[0] = 3 + 1 = 4.
17 * Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
18 * 
19 * Example 2:
20 * 
21 * Input: nums = [1,2,2,1], limit = 2
22 * Output: 2
23 * Explanation: In 2 moves, you can change nums to [<u>2</u>,2,2,<u>2</u>]. You cannot change any number to 3 since 3 > limit.
24 * 
25 * Example 3:
26 * 
27 * Input: nums = [1,2,1,2], limit = 2
28 * Output: 0
29 * Explanation: nums is already complementary.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	n == nums.length
35 * 	2 <= n <= 10^5
36 * 	1 <= nums[i] <= limit <= 10^5
37 * 	n is even.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/minimum-moves-to-make-array-complementary/
43// discuss: https://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn min_moves(nums: Vec<i32>, limit: i32) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1674() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.