⚠️ Connection to server is slow. High Latency detected...
❌ LeetCode Cloud Error: Server Unreachable. Please try again later.
LeetCode
Connected (-- ms)
U
Description  |  Editorial  |  Solutions  |  Submissions

1674. Minimum Moves to Make Array Complementary

Medium

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.

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.

Return the minimum number of moves required to make nums complementary.


Example 1:

Input: nums = [1,2,4,3], limit = 4
Output: 1
Explanation: In 1 move, you can change nums to [1,2,2,3].
nums[0] + nums[3] = 1 + 3 = 4.
nums[1] + nums[2] = 2 + 2 = 4.
Therefore, nums[i] + nums[n-1-i] = 4 for every i.
Java Auto
// class Solution { // private int add(int n1, int n2, int limit, int magic){ // if(n1 >= 1 && n1 <= limit){ // if((n2 < 1 || n2 > limit)){ // return 1; // } // } // else{ // if(n1+n2 == magic) // return 0; // else{ // if(n2 + limit < magic) // return 2; // return 1; // } // } // return 2; // } class Solution { public int minMoves(int[] nums, int limit) { int n = nums.length; // i need to find the mode of the sum number pairs which both are in the range of [1, limit] // then, for each pair, check if both not in range add 2 to move int mode = -1; HashMap<Integer, Integer> mp = new HashMap<>(); int right = n - 1; for(int left = 0; left < right; left++){ // implementation... } return 0; } }
Testcase >_ Test Result