**Problem Description**

The problem solvers have found a new Island for coding and named it as Philaland. These smart people were given a task to make purchase of items at the Island easier by distributing various coins with different value. Manish has come up with a solution that if we make coins category starting from $1 till the maximum price of item present on Island, then we can purchase any item easily. He added following example to prove his point.

Lets suppose the maximum price of an item is 5$ then we can make coins of {$1, $2, $3, $4, $5} to purchase any item ranging from $1 till $5. Now Manisha, being a keen observer suggested that we could actually minimize the number of coins required and gave following distribution {$1, $2, $3}. According to him any item can be purchased one time ranging from $1 to $5. Everyone was impressed with both of them.

Your task is to help Manisha come up with minimum number of denominations for any arbitrary max price in Philaland.

**Examples:**

Input:N = 10Output:4Explanation:

According to Manish {$1, $2, $3, … $10} must be distributed.

But as per Manisha only {$1, $2, $3, $4} coins are enough to purchase any item ranging from $1 to $10. Hence minimum is 4. Likewise denominations could also be {$1, $2, $3, $5}. Hence answer is still 4.

Input:N = 5Output:3Explanation:

According to Manish {$1, $2, $3, $4, $5} must be distributed.

But as per Manisha only {$1, $2, $3} coins are enough to purchase any item ranging from $1 to $5. Hence minimum is 3. Likewise denominations could also be {$1, $2, $4}. Hence answer is still 3.

**Approach: **The key observation of the problem is that any number can be represented as the powers two. Therefore, the minimum number of denomination required are –

**For Example:**

For N = 12,

If we choose the denominations as {1, 2, 4, 8}

Then every number up to 12 can be represented as –1 ==> 1

2 ==> 2

3 ==> 2 + 1

4 ==> 4

5 ==> 4 + 1

6 ==> 4 + 2

7 ==> 4 + 2 + 1

8 ==> 8

9 ==> 8 + 1

10 ==> 8 + 2

11 ==> 8 + 2 + 1

12 ==> 8 + 4

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// minimum number of denominations ` `// required for any number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum ` `// number of denomminations required ` `int` `findMinDenomin(` `int` `n) ` `{ ` ` ` `return` `log2(n) + 1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `// Function Call ` ` ` `cout << findMinDenomin(n); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# minimum number of denominations ` `# required for any number ` `from` `math ` `import` `log2, floor ` ` ` `# Function to find the minimum ` `# number of denomminations required ` `def` `findMinDenomin(n): ` ` ` ` ` `return` `log2(n) ` `+` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `10` ` ` ` ` `# Function call ` ` ` `print` `(floor(findMinDenomin(n))) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

4

**Performance Analysis:**

**Time Complexity:**O(logN)**Auxiliary Space:**O(1)

## Recommended Posts:

- Prime Fibonnaci | TCS Mockvita 2020
- Collision Course | TCS MockVita 2020
- Television Sets | TCS MockVita 2020
- Make a fair coin from a biased coin
- Coin Change | DP-7
- Frobenius coin problem
- Understanding The Coin Change Problem With Dynamic Programming
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Expected number of coin flips to get two heads in a row?
- Coin Change | BFS Approach
- Find the player who will win the Coin game
- Probability of getting K heads in N coin tosses
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Count of total Heads and Tails after N flips in a coin
- Count ways to distribute exactly one coin to each worker
- Probability of not getting two consecutive heads together in N tosses of coin
- Counting Rock Samples | TCS Codevita 2020
- Card Shuffle Problem | TCS Digital Advanced Coding Question
- 7 Best Coding Challenge Websites in 2020

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.