Open In App

Maximum candies two friends can eat

Last Updated : 24 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • X and Y represent the maximum calories two friends can consume,
  • cnt1 and cnt2 are the numbers of two types of candies,
  • C1 and C2 are the calories present in each candy of the two types of available candies.

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.

  • If the calorie value of candy 1 is greater than that of candy 2, then swap the calorie values and counts of both the candy types to ensure that the candy type with fewer calories is consumed first.
  • Initialize a variable maxCandies with 0, to store the maximum number of candies that can be eaten by the two friends.
  • Run a loop from i = 0 to i = cnt1 (number of candies of type 1) to check all possible combinations:
    • Check if the calories consumed by friend 1(i*C1) are greater than his maximum calorie capacity(X). If this is the case, further statements are not executed,
    • Calculate the number of type 1 candies consumed by friend 2 (candy1_frnd2). It is given by:
    • Calculate the calorie capacity of friend 1 and friend 2 which they can still consume (say left_frnd1 and left_frnd2 ). It can be given by:
    • Calculate the number of type 2 candies consumed by friend1 and friend2 respectively (say candy2_frnd1 and candy2_frnd2). It can be given by:
    • Update the maximum number of candies that can be consumed by the two friends:
  • Return the final value stored in maxCandies

Below is the implementation of the above approach:

C++




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




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




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




// 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.


Javascript




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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads