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: We can select rewards in optimal order. One optimal sequence: select 2 (total=2), then 3 (total=5), then 6 (total=11). Other values become unselectable. Maximum reward is 11.
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 Using Operations I INPUT rewardValues Array 1 i=0 1 i=1 3 i=2 3 i=3 Initial State: x = 0 (total reward) Selection Condition: Can only pick reward[i] if reward[i] > x [1, 1, 3, 3], n = 4 ALGORITHM STEPS 1 Sort Array [1,1,3,3] (ascending) 2 Pick 1 (x=0) 1 > 0? Yes! x = 0+1 = 1 3 Skip 1 (x=1) 1 > 1? No! Skip 4 Pick 3 (x=1) 3 > 1? Yes! x = 1+3 = 4 5 Skip 3 (x=4) 3 > 4? No! Skip Reward Progress: +1 +3 0 --> 1 --> 4 FINAL RESULT Maximum Total Reward 4 Items Selected: 1 OK + 3 OK Items Skipped: 1 skip 3 skip Key Insight: Sorting allows us to greedily pick rewards in ascending order. Each picked reward must be strictly greater than the current total. By processing smaller values first, we maximize chances to collect more rewards while satisfying the constraint: reward[i] > x. Time Complexity: O(n log n) TutorialsPoint - Maximum Total Reward Using Operations I | Greedy with Sorting Approach
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