Ctrl K

Largest Equal Group After Nudging

Medium
You are given an integer array values and an integer reach. Each element may be replaced at most once by any integer in the range [values[i] - reach, values[i] + reach]. After the replacements, pick as many elements as possible (from any positions) that are all equal. Return the largest possible number of such elements.
Example 1
values=[3, 11, 8, 15], reach=2 → 2

8 and 11 can both become 10 (or 9), but no three can be made equal: 8 and 15 differ by 7, more than 2 · 2. The answer is 2.

Example 2
values=[1, 10, 4, 7], reach=3 → 3

Turn 1, 4 and 7 into 4: shift the first by +3 and the third by -3. The 10 cannot reach them.

Example 3
values=[6], reach=0 → 1
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁵
  • 0 ≤ reach ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest k Values MediumEven Out With Increments MediumRug Over Painted Stretches MediumBridge the Coverage Gaps MediumNudge Values to Agree Medium
loading Python…
Input
values=[3, 11, 8, 15], reach=2
Expected
2

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