Maximize big when both big and small can be exchanged
Last Updated :
26 Feb, 2023
Given N Big Candies and M Small Candies. One Big Candy can be bought by paying X small candies. Alternatively, one big candy can be sold for Y small candies. The task is to find the maximum number of big candies that can be bought.
Examples:
Input: N = 3, M = 10, X = 4, Y = 2
Output: 5
8 small candies are exchanged for 2 big candies.
Input: N = 3, M = 10, X = 1, Y = 2
Output: 16
Sell all the initial big candies to get 6 small candies.
Now 16 small candies can be exchanged for 16 big candies.
In first example, Big candies cannot be sold for profit. So, only the remaining small candies can be exchanged for big candies.
In second example, Big candies can be sold for profit.
Approach: If initial big candies can be sold for profit i.e. X < Y then sell the big candies and update the count of small and big candies. Then, sell all of the updated small candies in order to buy big candies.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int max_candies( int bigCandies,
int smallCandies, int X, int Y)
{
if (X < Y)
{
smallCandies += Y * bigCandies;
bigCandies = 0;
}
bigCandies += (smallCandies / X);
return bigCandies;
}
int main()
{
int N = 3, M = 10;
int X = 4, Y = 2;
cout << (max_candies(N, M, X, Y));
return 0;
}
|
Java
class GFG {
static int max_candies( int bigCandies, int smallCandies,
int X, int Y)
{
if (X < Y) {
smallCandies += Y * bigCandies;
bigCandies = 0 ;
}
bigCandies += (smallCandies / X);
return bigCandies;
}
public static void main(String[] args)
{
int N = 3 , M = 10 ;
int X = 4 , Y = 2 ;
System.out.println(max_candies(N, M, X, Y));
}
}
|
Python3
def max_candies(bigCandies, smallCandies, X, Y):
if (X < Y):
smallCandies + = Y * bigCandies
bigCandies = 0
bigCandies + = (smallCandies / / X)
return bigCandies
N = 3
M = 10
X = 4
Y = 2
print (max_candies(N, M, X, Y))
|
C#
using System;
class GFG
{
static int max_candies( int bigCandies,
int smallCandies,
int X, int Y)
{
if (X < Y)
{
smallCandies += Y * bigCandies;
bigCandies = 0;
}
bigCandies += (smallCandies / X);
return bigCandies;
}
static public void Main ()
{
int N = 3, M = 10;
int X = 4, Y = 2;
Console.WriteLine(max_candies(N, M, X, Y));
}
}
|
PHP
<?php
function max_candies( $bigCandies ,
$smallCandies , $X , $Y )
{
if ( $X < $Y )
{
$smallCandies += $Y * $bigCandies ;
$bigCandies = 0;
}
$bigCandies += (int)( $smallCandies / $X );
return $bigCandies ;
}
$N = 3;
$M = 10;
$X = 4;
$Y = 2;
echo (max_candies( $N , $M , $X , $Y ));
?>
|
Javascript
<script>
function max_candies(bigCandies, smallCandies, X, Y)
{
if (X < Y)
{
smallCandies += Y * bigCandies;
bigCandies = 0;
}
bigCandies += parseInt(smallCandies / X, 10);
return bigCandies;
}
let N = 3, M = 10;
let X = 4, Y = 2;
document.write(max_candies(N, M, X, Y));
</script>
|
Time Complexity: O(1) as constant operations done.
Space Complexity: O(1) as no extra space has been used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...