Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum number of consecutive values you can make in Python
Suppose we have an array called coins with n elements, representing the coins that we own. The value of the ith coin is denoted as coins[i]. We can make some value x if we can select some of our n coins such that their values sum up to x. We have to find the maximum number of consecutive values that we can get with the coins starting from and including 0.
So, if the input is like coins = [1,1,3,4], then the output will be 10, because ?
0 = [] (empty selection)
1 = [1]
2 = [1,1]
3 = [3]
4 = [4]
5 = [4,1]
6 = [4,1,1]
7 = [4,3]
8 = [4,3,1]
9 = [4,3,1,1]
Algorithm
To solve this problem, we will follow these steps ?
Sort the
coinsarray in ascending orderInitialize
ans = 1(representing the next value we want to make)-
For each coin in coins, do ?
If
coin > ans, then we cannot make the consecutive valueans, so breakOtherwise, update
ans = ans + coin
Return
ans(the first value we cannot make)
How It Works
The key insight is that if we can make all values from 0 to k-1, and we have a coin of value c ? k, then we can make all values from 0 to k+c-1. By sorting coins and processing them greedily, we ensure optimal coverage.
Example
Let us see the following implementation to get better understanding ?
def solve(coins):
coins.sort()
ans = 1
for coin in coins:
if coin > ans:
break
ans += coin
return ans
coins = [1, 1, 3, 4]
print(solve(coins))
The output of the above code is ?
10
Step-by-Step Execution
Let's trace through the algorithm with coins = [1, 1, 3, 4] ?
def solve_with_trace(coins):
coins.sort()
print(f"Sorted coins: {coins}")
ans = 1
print(f"Initial ans: {ans}")
for coin in coins:
print(f"Processing coin: {coin}")
if coin > ans:
print(f"Coin {coin} > {ans}, breaking")
break
ans += coin
print(f"Updated ans: {ans}")
return ans
coins = [1, 1, 3, 4]
result = solve_with_trace(coins)
print(f"Final result: {result}")
The output shows the step-by-step process ?
Sorted coins: [1, 1, 3, 4] Initial ans: 1 Processing coin: 1 Updated ans: 2 Processing coin: 1 Updated ans: 3 Processing coin: 3 Updated ans: 6 Processing coin: 4 Updated ans: 10 Final result: 10
Another Example
Let's test with a different set of coins ?
def solve(coins):
coins.sort()
ans = 1
for coin in coins:
if coin > ans:
break
ans += coin
return ans
# Test with different examples
test_cases = [
[1, 1, 1, 4],
[1, 3],
[1, 1, 2, 5],
[2, 5]
]
for coins in test_cases:
result = solve(coins)
print(f"coins = {coins}, max consecutive values = {result}")
coins = [1, 1, 1, 4], max consecutive values = 8 coins = [1, 3], max consecutive values = 5 coins = [1, 1, 2, 5], max consecutive values = 10 coins = [2, 5], max consecutive values = 1
Conclusion
This greedy algorithm efficiently finds the maximum number of consecutive values by sorting coins and maintaining the next target value. The time complexity is O(n log n) due to sorting, and space complexity is O(1).
