Maximum Total Reward Using Operations I - Problem

You are given an integer array rewardValues of length n, representing the values of rewards. Initially, your total reward x is 0, and all indices are unmarked.

You are allowed to perform the following operation any number of times:

  • Choose an unmarked index i from the range [0, n - 1].
  • If rewardValues[i] is greater than your current total reward x, then add rewardValues[i] to x (i.e., x = x + rewardValues[i]), and mark the index i.

Return an integer denoting the maximum total reward you can collect by performing the operations optimally.

Input & Output

Example 1 — Basic Case
$ Input: rewardValues = [1,1,3,3]
Output: 4
💡 Note: Sort to [1,1,3,3]. Start with total=0. Select first 1 (1>0), total=1. Skip second 1 (1≤1). Select first 3 (3>1), total=4. Skip second 3 (3≤4). Maximum reward is 4.
Example 2 — All Selectable
$ Input: rewardValues = [1,6,4,3,2]
Output: 11
💡 Note: Sort to [1,2,3,4,6]. Start with total=0. Select 1 (total=1), select 2 (total=3), select 3 (total=6), skip 4 (4≤6), skip 6 (6≤6). Maximum reward is 6.
Example 3 — Single Element
$ Input: rewardValues = [5]
Output: 5
💡 Note: Only one element [5]. Since 5 > 0, we can select it. Maximum reward is 5.

Constraints

  • 1 ≤ rewardValues.length ≤ 2000
  • 1 ≤ rewardValues[i] ≤ 2000

Visualization

Tap to expand
Maximum Total Reward ProblemInput:1133Process:total = 0Select 1 (1 > 0)total = 1Skip 1 (1 ≤ 1)total = 1Select 3 (3 > 1)total = 4Skip 3 (3 ≤ 4)4Maximum Total Reward
Understanding the Visualization
1
Input
Array of reward values [1,1,3,3]
2
Process
Sort and greedily select rewards > current total
3
Output
Maximum total reward achievable: 4
Key Takeaway
🎯 Key Insight: Sort rewards and greedily select each reward that's greater than current total to maximize the final sum
Asked in
Meta 15 Amazon 12 Microsoft 8
17.0K Views
Medium Frequency
~25 min Avg. Time
680 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