Show problems as
Classic problem #2592MediumArrayTwo PointersGreedySorting

Maximize Greatness of an Array

Real-world version: How Many Places Can Be Bettered
Tracked in this browser only
Read Maximize Greatness of an Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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