Minimum Array Changes to Make Differences Equal

You are given an integer array nums of size n where n is even, and an integer k.

You can perform some changes on the array, where in one change you can replace any element in the array with any integer in the range from 0 to k.

You need to perform some changes (possibly none) such that the final array satisfies the following condition:

  • There exists an integer X such that abs(a[i] - a[n - i - 1]) = X for all 0 <= i < n.

Return the minimum number of changes required to satisfy the above condition.

Example 1
Inputnums = [1,0,1,2,4,3], k = 4
Output2
After replacing nums[1] with 2 and nums[3] with 3, every mirrored pair has absolute difference X = 2.
Example 2
Inputnums = [0,1,2,3,3,6,5,4], k = 6
Output2
After replacing nums[3] with 0 and nums[4] with 4, every mirrored pair has absolute difference X = 4.

Constraints

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

Asked at 2 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