The Maximum Number of Consecutive Values You Can Make coding problem gives you an array of coin denominations. Starting from 0, you want to form every integer value from 0 upward consecutively using subsets of the coins. Find the maximum value x such that you can form all integers from 0 to x-1 (i.e., 0 through x-1 inclusive). In other words, find the first integer you cannot form.
Infosys and Amazon include this problem because it has a beautifully elegant greedy solution that's non-obvious at first glance. The algorithm — sort coins, maintain a "reach" variable representing the maximum value you can currently form, and extend reach by each coin if it's ≤ reach+1 — is a classic greedy technique that rewards candidates who can think about "what values can I currently guarantee?" incrementally.
Greedy with sorted iteration: Sort the coins. Initialize reach = 0 (you can form all values from 0 to 0, i.e., just 0). For each coin c in sorted order:
c > reach + 1: you can't form reach + 1, so stop. Answer = reach + 1.c <= reach + 1: adding coin c extends your reach to reach + c. Update reach += c.After processing all coins, answer = reach + 1.
The key insight: if you can form all values [0, reach] and you have a coin c ≤ reach+1, you can now form all values [0, reach+c].
Coins: [1, 3, 4, 5]
You can form 0 and 1, but not 2. Answer = 2.
Coins: [1, 1, 2, 5]
For the Array Sorting Greedy interview pattern, the "reach extension" greedy is a powerful tool. It appears in problems like "jump game" variants and "coin reachability." The invariant to maintain is: "I can form all values in [0, reach]." Each sorted coin either extends this range or reveals a gap. Practice stating this invariant clearly in interviews — it demonstrates algorithmic maturity.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Eat Pizzas! | Medium | Solve | |
| Find Minimum Time to Finish All Jobs II | Medium | Solve | |
| Maximize Happiness of Selected Children | Medium | Solve | |
| Maximum Bags With Full Capacity of Rocks | Medium | Solve | |
| Maximum Element After Decreasing and Rearranging | Medium | Solve |