Watch 8 video solutions for Maximize Sum of At Most K Distinct Elements, a easy level problem involving Array, Hash Table, Greedy. This walkthrough by Programming Live with Larry has 340 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a positive integer array nums and an integer k.
Choose at most k elements from nums so that their sum is maximized. However, the chosen numbers must be distinct.
Return an array containing the chosen numbers in strictly descending order.
Example 1:
Input: nums = [84,93,100,77,90], k = 3
Output: [100,93,90]
Explanation:
The maximum sum is 283, which is attained by choosing 93, 100 and 90. We rearrange them in strictly descending order as [100, 93, 90].
Example 2:
Input: nums = [84,93,100,77,93], k = 3
Output: [100,93,84]
Explanation:
The maximum sum is 277, which is attained by choosing 84, 93 and 100. We rearrange them in strictly descending order as [100, 93, 84]. We cannot choose 93, 100 and 93 because the chosen numbers must be distinct.
Example 3:
Input: nums = [1,1,1,2,2,2], k = 6
Output: [2,1]
Explanation:
The maximum sum is 3, which is attained by choosing 1 and 2. We rearrange them in strictly descending order as [2, 1].
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1091 <= k <= nums.lengthProblem Overview: You are given an array of integers and a number k. The goal is to select at most k distinct elements such that their sum is as large as possible. Duplicate values cannot be counted multiple times, so the solution must ensure uniqueness while maximizing the total.
Approach 1: Brute Force with Distinct Tracking (Exponential Time)
The most direct idea is to try every possible subset of elements and keep only those containing at most k distinct values. For each valid subset, compute the sum and track the maximum. While this guarantees correctness, the number of subsets grows exponentially with the array size. Time complexity becomes O(2^n) and space complexity is O(n) due to recursion or subset storage. This approach is mainly useful for understanding the constraints but is impractical for real input sizes.
Approach 2: Hash Set + Sorting (Greedy) (O(n log n))
The key observation is that if you want the maximum possible sum with distinct elements, you should always choose the largest available unique values first. Start by removing duplicates using a hash set. This guarantees each number is counted once. Convert the set back to a list and sort it in descending order using a standard sorting algorithm. Then iterate through the sorted values and add the top k elements to the sum. The sorting step dominates the complexity, giving O(n log n) time and O(n) space.
This approach combines ideas from array processing and hash table deduplication. The greedy step works because larger numbers always contribute more to the final sum, so selecting the largest unique values first is optimal.
Approach 3: Hash Set + Max Heap (O(n log n))
Another option is to insert distinct values into a max heap instead of sorting the entire array. After removing duplicates using a hash set, push the numbers into a heap and extract the largest element up to k times. Heap insertion and extraction both take O(log n), producing overall time complexity O(n log n) and space complexity O(n). This technique is useful when the data stream arrives incrementally or when you prefer priority queue operations instead of full sorting.
The greedy reasoning remains the same: always take the largest available distinct value first. This aligns with typical patterns seen in greedy optimization and sorting-based selection problems.
Recommended for interviews: The hash set + sorting approach is the most expected solution. It clearly demonstrates two key skills: eliminating duplicates efficiently and applying a greedy selection strategy after sorting. Mentioning the brute force method shows you understand the search space, but implementing the sorting-based approach proves you can reduce the problem to a clean O(n log n) solution.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Subset Enumeration | O(2^n) | O(n) | Conceptual understanding or very small input sizes |
| Hash Set + Sorting (Greedy) | O(n log n) | O(n) | General case; simple and interview-friendly solution |
| Hash Set + Max Heap | O(n log n) | O(n) | Useful when extracting top elements incrementally instead of sorting |