Back to all solutions
#2592 - Maximize Greatness of an Array
Problem 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.
Solution
/**
* @param {number[]} nums
* @return {number}
*/
var maximizeGreatness = function(nums) {
nums.sort((a, b) => a - b);
let result = 0;
let i = 0;
let j = 1;
while (j < nums.length) {
if (nums[j] > nums[i]) {
result++;
i++;
j++;
} else {
j++;
}
}
return result;
};