Ctrl K

Nudge Values Into Line

Hard
You are given an integer array values and two integers reach and moves. You must make exactly moves moves. In each move you pick an element that has **never been picked before** and add any integer from -reach to reach to it (0 included). Return the largest number of equal elements the array can contain after all the moves.
Example 1
values=[2, 9, 6, 7], reach=2, moves=2 → 3

Shift 9 by -2 and 6 by +1 to get [2, 7, 7, 7]: three equal numbers.

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

The two 3s are already equal, and no other number is within 4 of them. 10 and 14 can also be made equal with one move. Either way the answer is 2.

Example 3
values=[5], reach=0, moves=0 → 1
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
  • 0 ≤ reach ≤ 10⁹
  • 0 ≤ moves ≤ len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Equal Values on a Budget HardEven Out With Increments MediumRug Over Painted Stretches MediumNudge Values to Agree MediumRichest Row of Houses Medium
loading Python…
Input
values=[2, 9, 6, 7], reach=2, moves=2
Expected
3

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