Problem Description
Given an even‐length integer array nums and an integer k, you can change any element (to any integer in the range [0, k]) an arbitrary number of times. Your goal is to achieve that there exists some nonnegative integer X such that for every symmetric pair (nums[i], nums[n-i-1]), the absolute difference |nums[i] - nums[n-i-1]| equals X. Return the minimum number of changes required.
Key Insights
-
We work with symmetric pairs; let m = n/2.
-
For each pair (a, b), the “current difference” is diff = |a - b|. If diff already equals the chosen X, no change is needed.
-
With one change on exactly one of the two numbers we may get the required difference X—but only when X is “reachable.” In particular, when keeping one element fixed, one may adjust the other to either (fixed + X) or (fixed - X) provided the new value remains in [0,k].
-
For each pair the best we can do is:
- 0 operations if X equals the original diff.
- 1 operation if X differs from diff but X is within “reach” by changing one number.
- 2 operations otherwise (because with two changes we can choose any pair having difference X as long as some valid pair exists in [0,k]).
-
A key trick is to pre‐compute, for every symmetric pair, the largest value T for which one of the two numbers can be “fixed” so that a single change on its partner is enough. In fact, for a pair (a, b) we have:
T = max(a, b, k - a, k - b)
so that for any X in [0, T] one change is enough (if X is not already equal to diff) while for X > T two changes are forced.
-
Then, the cost for that pair when choosing a target difference X is:
• 0 if X == diff
• 1 if X ≤ T and X != diff
• 2 if X > T -
We can “sweep” over all potential target values X in the range [0,k] efficiently using a difference array and a frequency table of the original diffs.
-
Finally, the total minimum cost equals the minimum over all X in [0,k] of:
Total cost(X) = (2 * m) – (# of pairs that can be fixed with one change for this X) – (number of pairs that already have diff == X).
Space and Time Complexity
Time Complexity: O(n + k)
Space Complexity: O(k)
(Here n is the length of nums and k is the given bound; note that k can be as large as 10^5.)
Solution
We start by noting that every symmetric pair (a, b) can be “fixed” as follows:
-
If the current absolute difference diff equals our target X, no change is needed.
-
Otherwise, we check if one change is enough. One change is sufficient if X is not “too large” compared to a and b (more precisely, if X ≤ T where T = max(a, b, k - a, k - b)). If X is larger than T, one change will not let you reach X (for example, if both numbers are 3 and k=6 then you cannot get a difference of 4 by changing only one element because 3+4 is 7 which is out of range and 3-4 is negative).
-
To efficiently try all possible X (from 0 to k), we initially assume every pair would cost 2 changes. Then, for each pair, for every X in [0, T] we “save” one change (using a difference array technique). Also, if the pair’s current difference is diff, we record an extra saving (i.e. cost becomes 0 rather than 1) when X equals diff.
-
After processing every pair, for each X in [0,k] the total cost becomes:
Total cost(X) = (2 * (n/2)) – (number of pairs with X ≤ T for that pair) – (frequency of X as the original difference)
and we take the minimum over all X.
The solution uses a difference array to accumulate the “one-change saves” per valid X interval and then a frequency array for pairs that need 0 changes. Finally, we iterate over X in [0,k] to compute the candidate cost and choose the minimum.