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 = 10
Output: 4
Explanation:
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 = 5
Output: 3
Explanation:
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 requiredint findMinDenomin(int n){Â Â Â Â return log2(n) + 1;}Â
// Driver Codeint main(){Â Â Â Â int n = 10;Â
    // Function Call    cout << findMinDenomin(n);    return 0;} |
Java
// Java implementation to find the // minimum number of denominations // required for any numberimport java.io.*;class GFG {       // Function to find the minimum     // number of denomminations required     static int findMinDenomin(int n)     {        return ((int)(Math.log(n)/Math.log(2))+1);    }       // Driver Code     public static void main (String[] args)     {        int n = 10;               // Function Call         System.out.println(findMinDenomin(n));    }}Â
//Â This code is contributed by avanitrachhadiya2155 |
Python3
# Python3 implementation to find the# minimum number of denominations# required for any numberfrom math import log2, floorÂ
# Function to find the minimum# number of denomminations requireddef findMinDenomin(n):Â
    return log2(n) + 1Â
# Driver Codeif __name__ == '__main__':Â Â Â Â Â Â Â Â Â n = 10Â
    # Function call    print(floor(findMinDenomin(n)))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation to find the // minimum number of denominations // required for any numberusing System;class GFG{Â
  // Function to find the minimum   // number of denomminations required   static int findMinDenomin(int n)   {    return ((int)(Math.Log(n)/Math.Log(2))+1);  }Â
  // Driver Code   static public void Main ()  {    int n = 10;Â
    // Function Call     Console.WriteLine(findMinDenomin(n));  }}Â
// This code is contributed by rag2127 |
Javascript
<script>// Javascript implementation to find the// minimum number of denominations// required for any numberÂ
// Function to find the minimum// number of denomminations requiredfunction findMinDenomin(n){Â Â Â Â Â Â Â Â return (Math.floor(Math.log(n)/Math.log(2)) + 1);}Â
// Driver Codelet n = 10;Â
 // Function Calldocument.write(findMinDenomin(n));Â
// This code is contributed by patel2127</script> |
4
Performance Analysis:
- Time Complexity: O(logN)
- Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] There you will find 14443 additional Info to that Topic: geeksforgeeks.org/philaland-coin-tcs-mockvita-2020/ […]
… [Trackback]
[…] Read More on on that Topic: geeksforgeeks.org/philaland-coin-tcs-mockvita-2020/ […]
… [Trackback]
[…] Here you will find 17398 more Info on that Topic: geeksforgeeks.org/philaland-coin-tcs-mockvita-2020/ […]
… [Trackback]
[…] Here you will find 90955 more Information to that Topic: geeksforgeeks.org/philaland-coin-tcs-mockvita-2020/ […]