All problems
1066MediumArrayTwo PointersGreedySorting

How Many Places Can Be Bettered

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2592Maximize Greatness of an Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A list of whole numbers reads values. Rearrange the same values into any order you like and lay the rearrangement alongside the original.

A position betters the original when the rearrangement's value there is strictly larger than the original's.

Return the most positions that can be made to better the original at once.

Examples

Example 1

Input
values = [2, 2, 2]
Output
0

Every value is the same, so no rearrangement puts a larger value anywhere.

Example 2

Input
values = [1, 1, 2, 2]
Output
2

The two twos can be laid over the two ones, bettering both of those positions, but nothing is left to better a two.

Example 3

Input
values = [5, 4, 3, 2, 1]
Output
4

Shifting every value one place along leaves each position holding the next value up, which betters all but one.

Constraints

  • 1 <= values.length <= 10^5
  • 0 <= values[i] <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def maximize_greatness(values: list[int]) -> int:
Java
public int maximizeGreatness(int[] values)
September 7
Apply