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++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` ` ` `// Function to return the maximum big ` ` ` `// candies that can be bought ` ` ` `int` `max_candies(` `int` `bigCandies, ` ` ` `int` `smallCandies,` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `// If initial big candies ` ` ` `// can be sold for profit ` ` ` `if` `(X < Y) ` ` ` `{ ` ` ` `smallCandies += Y * bigCandies; ` ` ` `bigCandies = 0; ` ` ` `} ` ` ` ` ` `// Update big candies that can be bought ` ` ` `bigCandies += (smallCandies / X); ` ` ` ` ` `return` `bigCandies; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `int` `main() ` ` ` `{ ` ` ` `int` `N = 3, M = 10; ` ` ` `int` `X = 4, Y = 2; ` ` ` `cout << (max_candies(N, M, X, Y)); ` ` ` `return` `0; ` ` ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the maximum big candies ` ` ` `// that can be bought ` ` ` `static` `int` `max_candies(` `int` `bigCandies, ` `int` `smallCandies, ` ` ` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `// If initial big candies can be sold for profit ` ` ` `if` `(X < Y) { ` ` ` ` ` `smallCandies += Y * bigCandies; ` ` ` `bigCandies = ` `0` `; ` ` ` `} ` ` ` ` ` `// Update big candies that can be bought ` ` ` `bigCandies += (smallCandies / X); ` ` ` ` ` `return` `bigCandies; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum big candies ` `# that can be bought ` `def` `max_candies(bigCandies, smallCandies, X, Y): ` ` ` ` ` `# If initial big candies can ` ` ` `# be sold for profit ` ` ` `if` `(X < Y): ` ` ` ` ` `smallCandies ` `+` `=` `Y ` `*` `bigCandies ` ` ` `bigCandies ` `=` `0` ` ` ` ` `# Update big candies that can be bought ` ` ` `bigCandies ` `+` `=` `(smallCandies ` `/` `/` `X) ` ` ` ` ` `return` `bigCandies ` ` ` `# Driver code ` `N ` `=` `3` `M ` `=` `10` `X ` `=` `4` `Y ` `=` `2` `print` `(max_candies(N, M, X, Y)) ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the maximum ` ` ` `// big candies that can be bought ` ` ` `static` `int` `max_candies(` `int` `bigCandies, ` ` ` `int` `smallCandies, ` ` ` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `// If initial big candies ` ` ` `// can be sold for profit ` ` ` `if` `(X < Y) ` ` ` `{ ` ` ` `smallCandies += Y * bigCandies; ` ` ` `bigCandies = 0; ` ` ` `} ` ` ` ` ` `// Update big candies that can be bought ` ` ` `bigCandies += (smallCandies / X); ` ` ` ` ` `return` `bigCandies; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `N = 3, M = 10; ` ` ` `int` `X = 4, Y = 2; ` ` ` `Console.WriteLine(max_candies(N, M, X, Y)); ` ` ` `} ` `} ` ` ` `// This Code is contributed by ajit... ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the maximum big ` `// candies that can be bought ` `function` `max_candies(` `$bigCandies` `, ` ` ` `$smallCandies` `, ` `$X` `, ` `$Y` `) ` `{ ` ` ` `// If initial big candies can be ` ` ` `// sold for profit ` ` ` `if` `(` `$X` `< ` `$Y` `) ` ` ` `{ ` ` ` ` ` `$smallCandies` `+= ` `$Y` `* ` `$bigCandies` `; ` ` ` `$bigCandies` `= 0; ` ` ` `} ` ` ` ` ` `// Update big candies that can be bought ` ` ` `$bigCandies` `+= (int)(` `$smallCandies` `/ ` `$X` `); ` ` ` ` ` `return` `$bigCandies` `; ` `} ` ` ` `// Driver code ` `$N` `= 3; ` `$M` `= 10; ` `$X` `= 4; ` `$Y` `= 2; ` ` ` `echo` `(max_candies(` `$N` `, ` `$M` `, ` `$X` `, ` `$Y` `)); ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Last digit of Product of two Large or Small numbers (a * b)
- Minimum capacity of small arrays needed to contain all element of the given array
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- N-th number which is both a square and a cube
- Program to display all alphabets from A to Z in uppercase and lowercase both
- Find two numbers with sum and product both same as N
- Maximize jobs that can be completed under given constraint
- Count ways to partition a string such that both parts have equal distinct characters
- Count of array elements which is smaller than both its adjacent elements
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Maximize the total profit of all the persons
- Maximize count of empty water bottles from N filled bottles
- Maximize the value of A by replacing some of its digits with digits of B
- Maximize the maximum among minimum of K consecutive sub-arrays
- Select numbers in such way to maximize the amount of money
- Number of ways to remove elements to maximize arithmetic mean
- Maximize removal of adjacent array elements based on their absolute value
- Maximize the Expression | Bit Manipulation

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.