
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find maximum number of consecutive values you can make in Python
Suppose we have an array called coins with n elements, and it is 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 = []
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]
To solve this, we will follow these steps −
sort the list coins
ans := 1
for each coin in coins, do
if coin > ans, then
come out from the loop
ans := ans + coin
return ans
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))
Input
[1,1,3,4]
Output
10
- Related Articles
- Program to find maximum number of people we can make happy in Python
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to find maximum number of eaten apples in Python
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Program to find maximum distance between a pair of values in Python
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Program to find maximum number of courses we can take based on interval time in Python?
- Program to find maximum number of non-overlapping substrings in Python
- Program to find maximum number of balanced groups of parentheses in Python
