Maximize Greatness: O(N log N) Algorithm with Linear Array Comparison
CodeIntuitionWe count the number from the biggest value and loop towards the next smaller number. If a smaller number is found, prev, the index of the next bigg
# Code
```swift []
class Solution {
    func maximizeGreatness(_ nums: [Int]) -> Int {
        let sortedNums = nums.sorted { $0 > $1 }
        var prev = 0 // count the bigger number that has its smaller counterpart
        var next = 1 // next smaller counterpart
        while next < nums.count, let j = sortedNums[next...]
        .firstIndex(where: { sortedNums[prev] > $0 }  ) {
            prev += 1
            next = j + 1
        }
        return prev
    }
}
```

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We count the number from the biggest value and loop towards the next smaller number. If a smaller number is found, $$prev$$, the index of the next bigger (but smaller then the first) number, is incremented, while $$next$$ points to the next smaller number. The loop only lasts until there still exists a smaller number. The comparison inside the loop always begins from the next smaller number and the main loop will terminate once no more valid pairings are found.

# Approach
<!-- Describe your approach to solving the problem. -->

1. Sorting: 
The input array nums is sorted in descending order using to efficiently find pairs where a larger number can "pair" with a smaller one.
2. Two Index Pointers:
$$prev$$ tracks the index of the larger number, while $$next$$ tracks the index of the smaller one that can be paired with the larger number.
3.  Loop and Comparison: 
The outside loop runs as long as $$next$$ is within the array bounds.
Inside, it searches the first index of the number smaller than $$sortedNums[prev]$$ as its counterpart, **starting from index $$next$$**.
If found, it increments $$prev$$ and updates $$next$$ to the next index after the smaller matched number. The outside loop terminates, after it iterates until the last element from $$next$$ **once**, which makes it linear.
4. Return Result: 
The function returns the value of $$prev$$, which represents the count of valid pairings.

# Complexity
- Time complexity: $$O(N \ log\ N)$$ worst case of the sorted method.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(N)$$ for $$sortedNums$$ because the original argument $$nums$$ is a let constant in Swift.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
['Swift']