Ctrl K

Mirror Pairs With a Common Sum

Medium
You get an array values of even length n and an integer limit. Split the array into mirror pairs: element i is paired with element n - 1 - i. The array is good when every mirror pair has the same sum. One change replaces any element with any integer from 1 to limit inclusive. Return the minimum number of changes that make the array good.
Example 1
values=[3, 1, 2, 2], limit=3 → 1

The pairs are 3 + 2 = 5 and 1 + 2 = 3. Replace the 1 with a 3 and both pairs sum to 5: one change.

Example 2
values=[1, 3, 3, 3], limit=3 → 1

The pairs sum to 4 and 6. Replacing the 1 with a 3 makes the first pair 3 + 3 = 6 as well, so the answer is 1.

Example 3
values=[1, 1, 4, 4], limit=4 → 0

Both pairs already sum to 5.

Constraints
  • 2 ≤ n = len(values) ≤ 10⁵, n is even
  • 1 ≤ values[i] ≤ limit ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Run with a Given Sum MediumA Stretch Divisible by k MediumAs Many Heads as Tails MediumStretches With an Exact Total MediumWindows With an Exact Alarm Count Medium
loading Python…
Input
values=[3, 1, 2, 2], limit=3
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.