Ctrl K

Even Out With Increments

Medium
You are given an integer array values and an integer budget. In one step you may pick any element and increase it by 1, and you can take at most budget steps in total. What is the largest number of equal elements the array can end up with?
Example 1
values=[2, 6, 5, 9], budget=4 → 2

Raise 5 to 6 (1 step) and there are two 6s. Three equal values are out of reach: lifting 2 and 5 to 6 costs 5 steps, and lifting 5 and 6 to 9 costs 7.

Example 2
values=[3, 1, 3, 2], budget=3 → 4

2 → 3 takes one step and 1 → 3 takes two, so all four values become 3.

Example 3
values=[10, 20, 30], budget=5 → 1

Any two values differ by more than 5, so no two can be made equal and the answer is 1.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
  • 0 ≤ budget ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRug Over Painted Stretches MediumRichest Row of Houses MediumNudge Values to Agree MediumFlowerbed Beauty on a Budget HardMost Items Within Budget Easy
loading Python…
Input
values=[2, 6, 5, 9], budget=4
Expected
2

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