Python Program for Philaland Coins Problems

Philaland Coin Problem

Philaland Coin Problem is one  of the questions of TCS CodeVita previous year coding competition. This problem is somewhat related to the coin distribution problem. In Philaland coin problem we have to devise a idea which help the user in obtaining the highest value with the minimum number of coins. Let’s see how to code a Python Program for Philaland Coin Problem

coin problem

Problem Statement

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.

Let’s 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.

  • Input Format
    • First line contains an integer T denoting the number of test cases.
    • Next T lines contains an integer N denoting the maximum price of the item present Philaland.

 

  • Output Format
    • For each test case print a single line denoting the minimum number of denominations of coins required.

 

  • Constraints
    • 1<=T<=100
    • 1<=N<=5000

 

Refer the Sample Output Formatting

Sample Input:

      2
     10
      5

Sample Output:

     4
     3

Explanation:

  • For test case 1, N=10.
    • 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.

 

  • For test case 2, N=5.
    • 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.

Python Program for Philaland Coins Problem

no_of_ways=int(input())
value = list(map(int,input().split()))
for i in value:
    count = 0
    #logic number of coins required will be one more than the earlier rupee value that was a power of 2
    while i>=1:
        i=i//2
        count=count+1
    print(count)
Output:
2
10 5
4
3

Philaland Coin Problem in few other Coding Languages

C

To find the solution of Philaland Coin Distribution Problem in C Programming language click on the button below:

C

C++

We don’t have the solution for this problem, you can contribute the answer of this code in C++ programming language, we post that answer on our page

Java

To find the solution of Philaland Coin Distribution Problem in Java Programming language click on the button below:

Java

5 comments on “Python Program for Philaland Coins Problems”


  • Ravi

    // Easy few line of c++ code
    #include

    using namespace std;

    int main()
    {
    //cout<>t; while(t–){
    int n=50;

    int temp=n;
    int cnt=0;
    int i=1;
    while(temp>0)
    {
    cnt++;temp=temp-i;i++;
    }
    int check= ((cnt-1)*(cnt-2))/2;
    if(check>=n)
    {
    cnt=cnt-1;
    }
    cout <<cnt<<endl;
    }

    return 0;
    }


  • Mohit

    t=int(input())
    while t>0:
    sum=0
    count=0
    n=int(input())
    for i in range(1,n+1):
    sum=sum+i
    count+=1
    if sum>=n:
    break
    print(count)
    t-=1


    • HelpPrepInsta

      It is easy for you Rakesh, but it maybe a bit difficult for other students, here we are giving a generic criteria to the questions