Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximize Sum of At Most K Distinct Elements

XPChallenge Points: 10
levelLevel: Easy

You are given an integer array nums containing positive integers and an integer k.
Your task is to select at most k distinct elements from nums such that their sum is maximized.
After selecting, return the chosen elements in strictly descending order.
If there are multiple possible sets with the same maximum sum, any valid set is acceptable.

Example 1:

Input: nums = [12, 45, 32, 45, 50], k = 3

Output: [50, 45, 32]

Example 2:

Input: nums = [10, 10, 20, 5, 15], k = 2

Output: [20, 15]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result