Minimum number of equal amount bags to collect at least M money
Last Updated :
12 Sep, 2023
Given unlimited number of coins of two denomination X and Y. Also given bags with capacity of N rupees, independent of number of coins. The task is to find the minimum number of bags such that each bag contains the same amount of rupees and sum of all the bags amount is at least M.
Examples :
Input : M = 27, N = 12, X = 2, Y = 5.
Output : 3
We put 2 coins of X, 1 coin of Y in each bag.
So we have 9 rupees in each bag and we need
at least 3 bags (Note that 27/9 = 3). There
is no way to obtain sum with lesser number
of bags.
Input : M = 45, N = 9, X = 4, Y = 5.
Output : 5
The task is to minimize the number of bags, thus need to maximize the amount in a bag such that amount in all bags is same. Suppose we take, ‘p’ number of X coins and ‘q’ number of Y coins, then the task is to maximize p*X + q*Y. And also, p*X + q*Y <= N.
Now, to find the maximum possible value of Left Hand Side of the equation, vary p from 0 to N/X, then find the maximum possible q for the particular p. Then, out of all such pairs, take the (p, q) pair which gives maximum value of p*X + q*Y.
Below is the implementation of above idea :
C++
#include<bits/stdc++.h>
using namespace std;
int minBags( int M, int N, int X, int Y)
{
int maxAmount = 0;
for ( int p = 0; p <= N/X; p++)
{
int q = (N - p * X) / Y;
maxAmount = max(maxAmount, p*X + q*Y);
}
int result = M/maxAmount;
result += (M % maxAmount == 0? 0: 1);
return result;
}
int main()
{
int M = 45, N = 9;
int X = 4, Y = 5;
cout << minBags(M, N, X, Y) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int minBags( int M, int N,
int X, int Y)
{
int maxAmount = 0 ;
for ( int p = 0 ; p <= N / X; p++)
{
int q = (N - p * X) / Y;
maxAmount = Math.max(maxAmount, p * X +
q * Y);
}
int result = M / maxAmount;
result += (M % maxAmount == 0 ? 0 : 1 );
return result;
}
static public void main (String[] args)
{
int M = 45 , N = 9 ;
int X = 4 , Y = 5 ;
System.out.println(minBags(M, N, X, Y));
}
}
|
Python3
def minBags(M, N, X, Y):
maxAmount = 0
for p in range ( 0 , int (N / X) + 1 , 1 ):
q = int ((N - p * X) / Y)
maxAmount = max (maxAmount, p * X + q * Y)
result = int (M / maxAmount)
if (M % maxAmount = = 0 ):
result + = 0
else :
result + = 1
return result
if __name__ = = '__main__' :
M = 45
N = 9
X = 4
Y = 5
print (minBags(M, N, X, Y))
|
C#
using System;
public class GFG
{
static int minBags( int M, int N,
int X, int Y)
{
int maxAmount = 0;
for ( int p = 0; p <= N / X; p++)
{
int q = (N - p * X) / Y;
maxAmount = Math.Max(maxAmount, p * X +
q * Y);
}
int result = M / maxAmount;
result += (M % maxAmount == 0? 0: 1);
return result;
}
static public void Main ()
{
int M = 45, N = 9;
int X = 4, Y = 5;
Console.WriteLine(minBags(M, N, X, Y));
}
}
|
PHP
<?php
function minBags( $M , $N , $X , $Y )
{
$maxAmount = 0;
for ( $p = 0; $p <= $N / $X ; $p ++)
{
$q = ( $N - $p * $X ) / $Y ;
$maxAmount = max( $maxAmount ,
$p * $X + $q * $Y );
}
$result = $M / $maxAmount ;
$result += ( $M % $maxAmount == 0? 0: 1);
return $result ;
}
$M = 45; $N = 9;
$X = 4 ; $Y = 5;
echo minBags( $M , $N , $X , $Y ) ;
?>
|
Javascript
<script>
function minBags(M, N, X, Y)
{
let maxAmount = 0;
for (let p = 0; p <= N / X; p++)
{
let q = (N - p * X) / Y;
maxAmount = Math.max(maxAmount, p * X +
q * Y);
}
let result = M / maxAmount;
result += (M % maxAmount == 0? 0: 1);
return result;
}
let M = 45, N = 9;
let X = 4, Y = 5;
document.write(minBags(M, N, X, Y));
</script>
|
Output :
5
Time Complexity : O(N/X)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...