Maximum candies two friends can eat
Last Updated :
24 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int maxCount( int X, int Y, int cnt1,
int cnt2, int C1, int C2)
{
if (C1 > C2) {
swap(C1, C2);
swap(cnt1, cnt2);
}
int ans = 0;
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;
}
int main()
{
int X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
int C1 = 5, C2 = 4;
int ans = maxCount(X, Y, cnt1,
cnt2, C1, C2);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void swap( int m, int n)
{
int temp = m;
m = n;
n = temp;
}
public static int maxCount( int X, int Y, int cnt1,
int cnt2, int C1, int C2)
{
if (C1 > C2) {
swap(C1, C2);
swap(cnt1, cnt2);
}
int ans = 0 ;
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;
}
public static void main(String[] args)
{
int X = 25 , Y = 36 , cnt1 = 10 , cnt2 = 6 ;
int C1 = 5 , C2 = 4 ;
int ans = maxCount(X, Y, cnt1, cnt2, C1, C2);
System.out.println(ans);
}
}
|
Python
def maxCount(X, Y, cnt1, cnt2, C1, C2):
if (C1 > C2):
C1, C2 = C2, C1
cnt1, cnt2 = cnt2, cnt1
ans = 0
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
X = 25
Y = 36
cnt1 = 10
cnt2 = 6
C1 = 5
C2 = 4
ans = maxCount(X, Y, cnt1, cnt2, C1, C2)
print (ans)
|
C#
using System;
class GFG {
static void swap( int m, int n)
{
int temp = m;
m = n;
n = temp;
}
static int maxCount( int X, int Y, int cnt1, int cnt2,
int C1, int C2)
{
if (C1 > C2) {
swap(C1, C2);
swap(cnt1, cnt2);
}
int ans = 0;
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;
}
public static void Main()
{
int X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
int C1 = 5, C2 = 4;
int ans = maxCount(X, Y, cnt1, cnt2, C1, C2);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
const maxCount = (X, Y, cnt1, cnt2, C1, C2) => {
if (C1 > C2) {
let temp1 = C1;
C1 = C2;
C2 = temp1;
let temp2 = cnt1;
cnt1 = cnt2;
cnt2 = temp2;
}
let ans = 0;
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;
}
let X = 25, Y = 36, cnt1 = 10, cnt2 = 6;
let C1 = 5, C2 = 4;
let ans = maxCount(X, Y, cnt1,
cnt2, C1, C2);
document.write(ans);
</script>
|
Time Complexity: O(N), where N represents the type of candies with a lower calorie value.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...