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.
Input: N = 3, M = 10, X = 4, Y = 2
8 small candies are exchanged for 2 big candies.
Input: N = 3, M = 10, X = 1, Y = 2
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:
# 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 [tabby title="C#"]
- Last digit of Product of two Large or Small numbers (a * b)
- Maximize the total profit of all the persons
- 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 the value of A by replacing some of its digits with digits of B
- Count the nodes of the given tree whose weight has X as a factor
- Count the nodes of the given tree whose weighted string is a palindrome
- Difference Between BFS and DFS
- Multiply a number by 15 without using * and / operators
- Count the nodes in the given tree whose weight is even parity
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Print all leaf nodes of an n-ary tree using DFS
- Count the nodes in the given tree whose weight is a power of two
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.