Maximum number of mangoes that can be bought - GeeksforGeeks

30 Jun, 2021
Given two integers W and C, representing the number of watermelons and coins, the task is to find the maximum number of mangoes that can be bought given that each mango costs 1 watermelon and X coins and y coins can be earned selling a watermelon.
Examples:
Input: W = 10, C = 10, X = 1, Y = 1Output: 10Explanation: The most optimal way is to use 10 watermelons and 10 coins to buy 10 mangoes. Hence, the maximum number of mangoes that can be bought is 10.
Input: W = 4, C = 8, X = 4, Y = 4Output: 3Explanation: The most optimal way is to sell one watermelon. Then, the number of coins increases by 4. Therefore, the total number of coins becomes 12. Therefore, 3 watermelons and 12 coins can be used to buy 3 mangoes. Hence, the maximum number of mangoes that can be bought is 3.
Approach: This problem can be solved using binary search. The idea is to find the maximum number of mangoes in the search space. Follow the steps below to solve the problem:
Initialize a variable ans as 0 to store the required result.
Initialize two variables l as 0, r as W to store the boundary regions of the search space for binary search.
Loop while l≤r and perform the following steps:Store the middle value in a variable mid as (l+r)/2.Check if mid number of mangoes can be bought using the given value of W, C, x, and y.If true, then update ans to mid and search in the right part of mid by updating l to mid+1. Otherwise, update the value of r to mid-1.
Store the middle value in a variable mid as (l+r)/2.
Check if mid number of mangoes can be bought using the given value of W, C, x, and y.
If true, then update ans to mid and search in the right part of mid by updating l to mid+1. Otherwise, update the value of r to mid-1.
Print the value of ans as the result.
Below is the implementation of the above approach:
C++14
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if mid number// of mangoes can be boughtbool check(int n, int m, int x, int y, int vl){    // Store the coins    int temp = m;     // If watermelons needed are greater    // than given watermelons    if (vl > n)        return false;     // Store remaining watermelons if vl    // watermelons are used to buy mangoes    int ex = n - vl;     // Store the value of coins if these    // watermelon get sold    ex *= y;     // Increment coins by ex    temp += ex;     // Number of mangoes that can be buyed    // if only x coins needed for one mango    int cr = temp / x;     // If the condition is satisfied,    // return true    if (cr >= vl)        return true;     // Otherwise return false    return false;} // Function to find the maximum number of mangoes// that can be bought by selling watermelonsint maximizeMangoes(int n, int m, int x, int y){    // Initialize the boundary values    int l = 0, r = n;     // Store the required result    int ans = 0;     // Binary Search    while (l <= r) {         // Store the mid value        int mid = l + (r - l) / 2;         // Check if it is possible to        // buy mid number of mangoes        if (check(n, m, x, y, mid)) {            ans = mid;            l = mid + 1;        }         // Otherwise, update r to mid -1        else            r = mid - 1;    }     // Return the result    return ans;} // Driver Codeint main(){    // Given Input    int W = 4, C = 8, x = 4, y = 4;     // Function Call    cout << maximizeMangoes(W, C, x, y);     return 0;}
// Java program for the above approachclass GFG{ // Function to check if mid number// of mangoes can be boughtstatic boolean check(int n, int m, int x,                     int y, int vl){         // Store the coins    int temp = m;     // If watermelons needed are greater    // than given watermelons    if (vl > n)        return false;     // Store remaining watermelons if vl    // watermelons are used to buy mangoes    int ex = n - vl;     // Store the value of coins if these    // watermelon get sold    ex *= y;     // Increment coins by ex    temp += ex;     // Number of mangoes that can be buyed    // if only x coins needed for one mango    int cr = temp / x;     // If the condition is satisfied,    // return true    if (cr >= vl)        return true;     // Otherwise return false    return false;} // Function to find the maximum number// of mangoes that can be bought by// selling watermelonsstatic int maximizeMangoes(int n, int m,                           int x, int y){         // Initialize the boundary values    int l = 0, r = n;     // Store the required result    int ans = 0;     // Binary Search    while (l <= r)    {                 // Store the mid value        int mid = l + (r - l) / 2;         // Check if it is possible to        // buy mid number of mangoes        if (check(n, m, x, y, mid))        {            ans = mid;            l = mid + 1;        }         // Otherwise, update r to mid -1        else            r = mid - 1;    }     // Return the result    return ans;} // Driver codepublic static void main(String[] args){         // Given Input    int W = 4, C = 8, x = 4, y = 4;     // Function Call    System.out.println(maximizeMangoes(W, C, x, y));}} // This code is contributed by abhinavjain194
# Python3 program for the above approach # Function to check if mid number# of mangoes can be boughtdef check(n, m, x, y, vl):         # Store the coins    temp = m     # If watermelons needed are greater    # than given watermelons    if (vl > n):        return False     # Store remaining watermelons if vl    # watermelons are used to buy mangoes    ex = n - vl     # Store the value of coins if these    # watermelon get sold    ex *= y     # Increment coins by ex    temp += ex     # Number of mangoes that can be buyed    # if only x coins needed for one mango    cr = temp // x     # If the condition is satisfied,    # return true    if (cr >= vl):        return True     # Otherwise return false    return False # Function to find the maximum number of mangoes# that can be bought by selling watermelonsdef maximizeMangoes(n, m, x, y):         # Initialize the boundary values    l = 0    r = n     # Store the required result    ans = 0     # Binary Search    while (l <= r):         # Store the mid value        mid = l + (r - l) // 2         # Check if it is possible to        # buy mid number of mangoes        if (check(n, m, x, y, mid)):            ans = mid            l = mid + 1         # Otherwise, update r to mid -1        else:            r = mid - 1     # Return the result    return ans # Driver Codeif __name__ == '__main__':         # Given Input    W = 4    C = 8    x = 4    y = 4     # Function Call    print(maximizeMangoes(W, C, x, y))     # This code is contributed by bgangwar59
// C# program for the above approachusing System; class GFG{ // Function to check if mid number// of mangoes can be boughtstatic bool check(int n, int m, int x,                  int y, int vl){         // Store the coins    int temp = m;     // If watermelons needed are greater    // than given watermelons    if (vl > n)        return false;     // Store remaining watermelons if vl    // watermelons are used to buy mangoes    int ex = n - vl;     // Store the value of coins if these    // watermelon get sold    ex *= y;     // Increment coins by ex    temp += ex;     // Number of mangoes that can be buyed    // if only x coins needed for one mango    int cr = temp / x;     // If the condition is satisfied,    // return true    if (cr >= vl)        return true;     // Otherwise return false    return false;} // Function to find the maximum number of mangoes// that can be bought by selling watermelonsstatic int maximizeMangoes(int n, int m, int x, int y){         // Initialize the boundary values    int l = 0, r = n;     // Store the required result    int ans = 0;     // Binary Search    while (l <= r)    {                 // Store the mid value        int mid = l + (r - l) / 2;         // Check if it is possible to        // buy mid number of mangoes        if (check(n, m, x, y, mid))        {            ans = mid;            l = mid + 1;        }         // Otherwise, update r to mid -1        else            r = mid - 1;    }     // Return the result    return ans;} // Driver Codepublic static void Main(){         // Given Input    int W = 4, C = 8, x = 4, y = 4;     // Function Call    Console.Write(maximizeMangoes(W, C, x, y));}}                // This code is contributed by ukasp
<script>// Javascript program for the above approach // Function to check if mid number// of mangoes can be boughtfunction check(n, m,  x, y,vl){    // Store the coins    var temp = m;     // If watermelons needed are greater    // than given watermelons    if (vl > n)        return false;     // Store remaining watermelons if vl    // watermelons are used to buy mangoes    var ex = n - vl;     // Store the value of coins if these    // watermelon get sold    ex *= y;     // Increment coins by ex    temp += ex;     // Number of mangoes that can be buyed    // if only x coins needed for one mango    var cr = parseInt(temp / x);     // If the condition is satisfied,    // return true    if (cr >= vl)        return true;     // Otherwise return false    return false;} // Function to find the maximum number of mangoes// that can be bought by selling watermelonsfunction maximizeMangoes(n, m, x, y){    // Initialize the boundary values    var l = 0, r = n;     // Store the required result    var ans = 0;     // Binary Search    while (l <= r) {         // Store the mid value        var mid = l + parseInt((r - l) / 2);         // Check if it is possible to        // buy mid number of mangoes        if (check(n, m, x, y, mid)) {            ans = mid;            l = mid + 1;        }         // Otherwise, update r to mid -1        else            r = mid - 1;    }     // Return the result    return ans;}var W = 4, C = 8, x = 4, y = 4; // Function Calldocument.write( maximizeMangoes(W, C, x, y)); //This code is contributed by SoumikMondal</script>
3
 
Time Complexity: O(log(W))Auxiliary Space: O(1)
 
ukasp
abhinavjain194
bgangwar59
SoumikMondal
Binary Search
Divide and Conquer
Mathematical
Searching
Searching
Mathematical
Divide and Conquer
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Binary Search In JavaScript
K-th Element of Two Sorted Arrays
Find a Fixed Point (Value equal to index) in a given array
Binary Search (bisect) in Python
Convex Hull using Divide and Conquer Algorithm
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7