Related Articles

Related Articles

Philaland Coin | TCS Mockvita 2020
  • Last Updated : 18 Jan, 2021

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 –

log_2 N + 1
 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the
// minimum number of denominations
// required for any number
import 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the
// minimum number of denominations
// required for any number
using 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

chevron_right


Output

4

Performance Analysis:

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

competitive-programming-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :