Philaland Coin | TCS Mockvita 2020
Last Updated :
17 Jun, 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 –
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++
#include <bits/stdc++.h>
using namespace std;
int findMinDenomin( int n)
{
return log2(n) + 1;
}
int main()
{
int n = 10;
cout << findMinDenomin(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findMinDenomin( int n)
{
return (( int )(Math.log(n)/Math.log( 2 ))+ 1 );
}
public static void main (String[] args)
{
int n = 10 ;
System.out.println(findMinDenomin(n));
}
}
|
Python3
from math import log2, floor
def findMinDenomin(n):
return log2(n) + 1
if __name__ = = '__main__' :
n = 10
print (floor(findMinDenomin(n)))
|
C#
using System;
class GFG
{
static int findMinDenomin( int n)
{
return (( int )(Math.Log(n)/Math.Log(2))+1);
}
static public void Main ()
{
int n = 10;
Console.WriteLine(findMinDenomin(n));
}
}
|
Javascript
<script>
function findMinDenomin(n)
{
return (Math.floor(Math.log(n)/Math.log(2)) + 1);
}
let n = 10;
document.write(findMinDenomin(n));
</script>
|
Performance Analysis:
- Time Complexity: O(logN)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...