Maximize Greatness of an Array - Problem

Imagine you have an array of numbers, and you want to create the most impressive rearrangement possible! You're given a 0-indexed integer array nums, and you can permute (rearrange) it into any order you want to create a new array perm.

The greatness of your arrangement is defined as the number of positions where perm[i] > nums[i] - essentially, how many elements in your rearranged array are greater than the corresponding elements in the original array.

Goal: Return the maximum possible greatness you can achieve by optimally rearranging the array.

Example: If nums = [1,3,5,2,1,3,1], you could rearrange it to perm = [3,5,1,3,2,1,1]. Comparing position by position: 3>1 ✓, 5>3 ✓, 1>5 ✗, 3>2 ✓, 2>1 ✓, 1>3 ✗, 1>1 ✗. That gives us a greatness of 4!

Input & Output

example_1.py — Basic Case
$ Input: [1,3,5,2,1,3,1]
Output: 4
💡 Note: We can rearrange nums into [3,5,1,3,2,1,1]. Comparing with original: 3>1✓, 5>3✓, 1>5✗, 3>2✓, 2>1✓, 1>3✗, 1>1✗. Total greatness = 4.
example_2.py — All Same Elements
$ Input: [1,1,1,1,1]
Output: 0
💡 Note: Since all elements are identical, no matter how we rearrange, we can never have perm[i] > nums[i] at any position. Greatness = 0.
example_3.py — Perfect Ascending
$ Input: [1,2,3,4,5]
Output: 4
💡 Note: We can arrange as [2,3,4,5,1]. This gives us: 2>1✓, 3>2✓, 4>3✓, 5>4✓, 1>5✗. Maximum possible greatness = 4.

Visualization

Tap to expand
🏆 Chess Tournament: Maximize VictoriesOriginal Team (Sorted):1112335Rearranged Team (Sorted):1112335Strategic Matches:1 vs 1 ❌1 vs 2 ✓1 vs 3 ✓2 vs 3 ✓3 vs 5 ✓🎯 Result: 4 Victories Out of 7 MatchesGreedy Strategy: Always use the weakest player who can win!💡 Time: O(n log n) | Space: O(n)
Understanding the Visualization
1
Rank Players
Sort both rosters by skill level from weakest to strongest
2
Strategic Pairing
For each player in the original roster, find the weakest player from the rearranged roster who can still beat them
3
Greedy Matching
Always use the weakest possible winning player - this saves stronger players for tougher opponents
4
Count Victories
Sum up all the successful matches to get maximum greatness
Key Takeaway
🎯 Key Insight: The greedy approach works because using the smallest possible winning element for each match preserves stronger elements for future tougher opponents, maximizing overall victories!

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

Sorting both arrays takes O(n log n), then single pass with two pointers takes O(n)

n
2n
Linearithmic
Space Complexity
O(n)

Need extra space to store the sorted copy of the array

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 109
  • The array can contain duplicate elements
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen