- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

[1,1,3,4]

10

- Related Questions & Answers
- 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 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 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 boxes we can fit inside another boxes 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
- Program to find maximum distance between a pair of values in Python
- How can you make people listen to you?
- Program to find maximum number of courses we can take based on interval time in Python?

Advertisements