Maximum Number of Consecutive Values You Can Make - Problem
You are given an integer array coins of length n which represents the n coins that you own. The value of the ith coin is coins[i].
You can make some value x if you can choose some of your n coins such that their values sum up to x.
Return the maximum number of consecutive integer values that you can make with your coins starting from and including 0.
Note that you may have multiple coins of the same value.
Input & Output
Example 1 — Basic Case
$
Input:
coins = [1,1,4]
›
Output:
3
💡 Note:
You can make 0 (use no coins), 1 (use one coin), 2 (use both 1-coins), but cannot make 3. So consecutive values are [0,1,2] with count 3.
Example 2 — Single Coin
$
Input:
coins = [1,3]
›
Output:
2
💡 Note:
You can make 0 (no coins), 1 (use coin 1), but cannot make 2. The coin 3 is too large to fill the gap. Count = 2.
Example 3 — Larger Gap
$
Input:
coins = [1,1,1,4]
›
Output:
8
💡 Note:
With three 1's you can make [0,1,2,3]. Adding coin 4 extends to [0,1,2,3,4,5,6,7]. Count = 8.
Constraints
- 1 ≤ coins.length ≤ 4 × 104
- 1 ≤ coins[i] ≤ 4 × 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of coin values [1,1,4]
2
Process
Find all possible sums and count consecutive from 0
3
Output
Maximum consecutive count: 3
Key Takeaway
🎯 Key Insight: Sort coins and greedily extend consecutive range - if coin ≤ current_reach+1, we can extend further
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code