Open In App

Maximum candies two friends can eat

Given six integers X, Y, cnt1, cnt2, C1, and C2, where: 

The task is to find the maximum number of candies the two friends can eat.



Examples:

Input: X = 25, Y = 36, cnt1 = 10, cnt2 = 6, C1 = 5, C2 = 4
Output: 13
Explanation: Candies will be eaten in the following manner.
Friend 1 can eat 5 candies of type 1: 5*5 = 25 <= 25
Friend 2 can eat all 6 candies of type 2 and 2 candies of type 1: 6*4 + 2*5 = 34 <= 36
Thus, total candies = 5 + 6 + 2 = 13.

Input: X = 452, Y = 225, cnt1 = 50, cnt2 = 125, C1 = 20, C2 = 30
Output: 33

 

Approach: This problem is observation-based and can be solved by using the Greedy Approach and trying all possible combinations of the candies two friends can consume from given cnt1 and cnt2 candies and trying to finish the candies with lower calory value first. 



Follow the steps below to solve the given problem.

Below is the implementation of the above approach:




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum count
int maxCount(int X, int Y, int cnt1,
             int cnt2, int C1, int C2)
{
    // If C1 > C2, swap them
    if (C1 > C2) {
        swap(C1, C2);
        swap(cnt1, cnt2);
    }
 
    int ans = 0;
 
    // Loop to find the
    // maximum count of candies
    for (int i = 0; i <= cnt1; i++) {
        if (i * C1 > X) {
            continue;
        }
        int ss = min(Y / C1, cnt1 - i);
        int left_wtf = X - i * C1;
        int left_wts = Y - ss * C1;
 
        int af = min(left_wtf / C2, cnt2);
        int as = min(left_wts / C2,
                     cnt2 - af);
 
        ans = max(ans, i + ss + af + as);
    }
    return ans;
}
 
// Driver code
int main()
{
    int X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
    int C1 = 5, C2 = 4;
 
    // Function call
    int ans = maxCount(X, Y, cnt1,
                       cnt2, C1, C2);
    cout << ans << endl;
    return 0;
}




// JAVA code to implement the approach
import java.util.*;
class GFG {
  public static void swap(int m, int n)
  {
    int temp = m;
    m = n;
    n = temp;
  }
 
  // Function to find the maximum count
  public static int maxCount(int X, int Y, int cnt1,
                             int cnt2, int C1, int C2)
  {
 
    // If C1 > C2, swap them
    if (C1 > C2) {
      swap(C1, C2);
      swap(cnt1, cnt2);
    }
 
    int ans = 0;
 
    // Loop to find the
    // maximum count of candies
    for (int i = 0; i <= cnt1; i++) {
      if (i * C1 > X) {
        continue;
      }
      int ss = Math.min(Y / C1, cnt1 - i);
      int left_wtf = X - i * C1;
      int left_wts = Y - ss * C1;
 
      int af = Math.min(left_wtf / C2, cnt2);
      int as = Math.min(left_wts / C2, cnt2 - af);
 
      ans = Math.max(ans, i + ss + af + as);
    }
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
    int C1 = 5, C2 = 4;
 
    // Function call
    int ans = maxCount(X, Y, cnt1, cnt2, C1, C2);
    System.out.println(ans);
  }
}
 
// This code is contributed by Taranpreet




# Python code to implement the approach
 
# Function to find the maximum count
def maxCount(X, Y, cnt1, cnt2, C1, C2):
 
    # If C1 > C2, swap them
    if (C1 > C2):
        C1, C2 = C2, C1
        cnt1, cnt2 = cnt2, cnt1
 
    ans = 0
 
    # Loop to find the
    # maximum count of candies
    for i in range(0, cnt1 + 1):
        if (i * C1 > X):
            continue
 
        ss = min(Y // C1, cnt1 - i)
        left_wtf = X - i * C1
        left_wts = Y - ss * C1
 
        af = min(left_wtf // C2, cnt2)
        as_ = min(left_wts // C2, cnt2 - af)
 
        ans = max(ans, i + ss + af + as_)
 
    return ans
 
# Driver code
X = 25
Y = 36
cnt1 = 10
cnt2 = 6
C1 = 5
C2 = 4
 
# Function call
ans = maxCount(X, Y, cnt1, cnt2, C1, C2)
print(ans)
 
# This code is contributed by Samim Hossain Mondal.




// C# code to implement the approach
using System;
class GFG {
  static void swap(int m, int n)
  {
    int temp = m;
    m = n;
    n = temp;
  }
 
  // Function to find the maximum count
  static int maxCount(int X, int Y, int cnt1, int cnt2,
                      int C1, int C2)
  {
 
    // If C1 > C2, swap them
    if (C1 > C2) {
      swap(C1, C2);
      swap(cnt1, cnt2);
    }
 
    int ans = 0;
 
    // Loop to find the
    // maximum count of candies
    for (int i = 0; i <= cnt1; i++) {
      if (i * C1 > X) {
        continue;
      }
      int ss = Math.Min(Y / C1, cnt1 - i);
      int left_wtf = X - i * C1;
      int left_wts = Y - ss * C1;
 
      int af = Math.Min(left_wtf / C2, cnt2);
      int as_ = Math.Min(left_wts / C2, cnt2 - af);
 
      ans = Math.Max(ans, i + ss + af + as_);
    }
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
    int C1 = 5, C2 = 4;
 
    // Function call
    int ans = maxCount(X, Y, cnt1, cnt2, C1, C2);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.




<script>
    // JavaScript code to implement the approach
 
 
    // Function to find the maximum count
    const maxCount = (X, Y, cnt1, cnt2, C1, C2) => {
        // If C1 > C2, swap them
        if (C1 > C2) {
 
            let temp1 = C1;
            C1 = C2;
            C2 = temp1;
 
            let temp2 = cnt1;
            cnt1 = cnt2;
            cnt2 = temp2;
 
        }
 
        let ans = 0;
 
        // Loop to find the
        // maximum count of candies
        for (let i = 0; i <= cnt1; i++) {
            if (i * C1 > X) {
                continue;
            }
            let ss = Math.min(parseInt(Y / C1), cnt1 - i);
            let left_wtf = X - i * C1;
            let left_wts = Y - ss * C1;
 
            let af = Math.min(parseInt(left_wtf / C2), cnt2);
            let as = Math.min(parseInt(left_wts / C2),
                cnt2 - af);
 
            ans = Math.max(ans, i + ss + af + as);
        }
        return ans;
    }
 
    // Driver code
 
    let X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
    let C1 = 5, C2 = 4;
 
    // Function call
    let ans = maxCount(X, Y, cnt1,
        cnt2, C1, C2);
    document.write(ans);
 
// This code is contributed by rakeshsahni
 
</script>

 
 

Output
13

 

Time Complexity: O(N), where N represents the type of candies with a lower calorie value.
Auxiliary Space: O(1)

 


Article Tags :