Minimum Moves to Make Array Complementary

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.

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

Example 1
Inputnums = [1,2,4,3], limit = 4
Output1
In 1 move, you can change nums to [1, 2, 2, 3], making every mirrored pair sum to 4.
Example 2
Inputnums = [1,2,2,1], limit = 2
Output2
In 2 moves, you can change nums to [2, 2, 2, 2], and values greater than limit are not allowed.

Constraints

  • n == nums.length
  • 2 <= n <= 10^5
  • 1 <= nums[i] <= limit <= 10^5
  • n is even.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate