Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Monday, May 22, 2017

Minimum Notes Required for money N


Problem :

Here we are having two integers n,k total money and no. of notes we can use 
We need to find out how much money we can have(Maximum with given denomination according to old indian currency) 

Output :

 Print remaining money

Explanation :


Here Greedy algorithm is used we are checking with maximum denomination and if money is greater then use denomination if not then check with lower denomination


Code:


import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
int money[]={1,2,5,10,20,50,100,500,1000};
while(t-->0)
{
   int n=ab.nextInt();
   int k=ab.nextInt();
   int i=8;
   while(n>0 && k-->0)
   {
       if(n>=money[i])
       {
           //System.out.println("salary "+n+" used denomination "+money[i]+" k"+k);
           n-=money[i];
         
       }
       else
       {
       if(i>0)
       {
       i--;
       k++;
       }
       }
      // System.out.println("Value of i "+i);
   }
   System.out.println(n);
}
}
}

0 Comments:

Post a Comment

Stats