Ctrl K

Equal Mirrored Gaps

Medium
You get an array values of even length n and an integer limit. Split the array into mirrored pairs: the first element with the last, the second with the second-to-last, and so on. In one change you may overwrite any element with any integer from 0 to limit inclusive. The goal is for every mirrored pair to have the same absolute difference: there must be some X such that |values[i] - values[n - 1 - i]| = X for every i. You pick X. Return the minimum number of changes.
Example 1
values=[3, 1, 4, 4, 0, 2], limit=5 → 1

The mirrored pairs are (3, 2), (1, 0) and (4, 4). The first two already differ by 1; in the third, replacing one 4 with 5 does the job. One change.

Example 2
values=[0, 0, 1, 5, 5, 0], limit=5 → 2

The gaps in pairs (0, 0), (0, 5), (1, 5) are 0, 5 and 4, all different, so at most one pair can stay as is. Each of the other two needs one change.

Constraints
  • 2 ≤ n = len(values) ≤ 10⁵, n is even
  • 0 ≤ 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, 4, 4, 0, 2], limit=5
Expected
1

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