2592. Maximize Greatness of an Array

2592. Maximize Greatness of an Array

Description

You are given a 0-indexed integer array nums. You are allowed to permute nums into a new array perm of your choosing.

We define the greatness of nums be the number of indices 0 <= i < nums.length for which perm[i] > nums[i].

Return the maximum possible greatness you can achieve after permuting nums.

Example 1:

1
2
3
4
Input: nums = [1,3,5,2,1,3,1]
Output: 4
Explanation: One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.

Example 2:

1
2
3
4
Input: nums = [1,2,3,4]
Output: 3
Explanation: We can prove the optimal perm is [2,3,4,1].
At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.

Constraints:

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

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int maximizeGreatness(vector<int>& nums) {
ranges::sort(nums);
int left = 0, right = 0, n = nums.size(), res = 0;
while (right < n) {
int l = nums[left++];
while (right < n && nums[right] <= l) {
right++;
}
if (right < n) {
res++;
right++;
}
}
return res;
}
};