Advertisement

Maximum Number of Consecutive Values You Can Make - LeetCode 1798 Solution

Maximum Number of Consecutive Values You Can Make - Complete Solution Guide

Maximum Number of Consecutive Values You Can Make is LeetCode problem 1798, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array coins of length n which represents the n coins that you own. The value of the i th 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. Example 1: Input: coins = [1,3] Output: 2 Explanation: You can make the following valu

Detailed Explanation

The problem asks us to find the maximum number of consecutive integer values that can be made using a given set of coins, starting from 0. We are given an array of coin values, and we can choose any combination of these coins to form a sum. The goal is to find the largest consecutive sequence of numbers (starting from 0) that we can create using these sums. For example, if coins = [1, 3], we can make 0 (no coins), 1 (using the 1 coin), but we cannot make 2. So the answer is 2. If coins = [1, 1, 1, 4], we can make 0, 1, 2, 3, 4, 5, 6, 7. Thus the answer is 8.

Solution Approach

The solution uses a greedy approach. First, the coins array is sorted in ascending order. This ensures that we consider smaller denominations first, which is crucial for the greedy strategy. We maintain a variable 'reachable' to represent the maximum consecutive value we can make starting from 0. We iterate through the sorted coins. For each coin, if its value is greater than 'reachable', it means we cannot extend the consecutive sequence any further, and we can stop. Otherwise, we add the coin's value to 'reachable' to extend the sequence.

Step-by-Step Algorithm

  1. Step 1: Sort the `coins` array in ascending order.
  2. Step 2: Initialize a variable `reachable` to 1. This represents the fact that we can always make 0 (by taking no coins), so the consecutive range starts at 0, making the maximum consecutive value at least 1 (the value 0).
  3. Step 3: Iterate through the sorted `coins` array.
  4. Step 4: For each `coin`, check if `coin > reachable`. If it is, break out of the loop because we can no longer extend the consecutive range.
  5. Step 5: If `coin <= reachable`, add `coin` to `reachable`. This extends the consecutive range because now we can make all values from 0 to `reachable + coin - 1`.
  6. Step 6: After iterating through all the coins (or breaking the loop), return the final value of `reachable`.

Key Insights

  • Insight 1: Sorting the coins is crucial because it allows us to greedily determine the maximum reachable value. We process the coins in ascending order.
  • Insight 2: The key idea is to maintain a 'reachable' variable, which represents the maximum consecutive value we can make so far (starting from 0). If the next coin's value is greater than 'reachable', we can no longer extend the consecutive sequence.
  • Insight 3: If a coin's value is less than or equal to the current 'reachable' value, we can add the coin to all the values from 0 to 'reachable' to extend the range of consecutive values we can make.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy, Sorting.

Companies

Asked at: Infosys.