Skip to main content
Back to problems
#1798
Medium Algorithms

Maximum number of consecutive values you can make

Array Greedy Sorting
63.5% acceptance
Feb 25, 2026
865
60
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.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn get_maximum_consecutive(mut coins: Vec<i32>) -> i32 {
    coins.sort_unstable();
    // reach = maximum consecutive value we can form [0..reach]
    let mut reach: i32 = 0;
    for c in coins {
      if c > reach + 1 {
        break;
      }
      reach += c;
    }
    reach + 1
  }
}