Count maximum number of consumable candies
Given two arrays A[] and B[] consisting of N integers representing the amount of each type of candies and maximum consumable limit respectively, and an integer M which represents the number of unknown candies added, the task is to find the maximum count of candies one person can consume in a blindfold.
Examples:
Input: A[] = {4, 5, 2, 3}, B[] = {8, 13, 6, 4}, M = 5
Output: 4
Explanation: Directly consume all 3 candies of 4th type and consume one more candy which can be any of type. Therefore, one can only consume total of 4 candies safely.
Input: A[] = {2, 4, 1, 9, 6}, B[] = {8, 7, 3, 12, 7}, M = 0
Output: 2
Explanation: One can directly consume all candies as all types of candies are within safe limits.
Approach: The given problem can be solved based on the following observations:
- If for every type of candies, A[i] + M ? B[i], then it is safe to consume all available candies.
- Otherwise, one can only consume minimum of min(A[i] + M, B[i]) for all 0 ? i < N.
Follow the steps below to solve the problem:
- Initialize two variables, say ans and total, to store the count of maximum candies safe to consume and the total count of candies
- Initialize a variable, say allSafe = true, to check if all types of candies are safe to consume or not.
- Traverse over the range [0, N – 1] and if A[i] + M > B[i], then set allSafe = false and update ans = min(ans, B[i]). Otherwise, update ans = min(ans, A[i]).
- If allSafe is true, then print the total sum of array A[].
- Otherwise, print the result in ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumCandy( int candies[],
int safety[],
int N, int M)
{
int total = 0;
int ans = INT_MAX;
bool all_safe = true ;
for ( int i = 0; i < N; i++) {
if (candies[i] + M > safety[i]) {
all_safe = false ;
ans = min(ans, safety[i]);
}
else {
ans = min(ans, candies[i] + M);
}
total += candies[i];
}
if (all_safe)
return total;
else
return ans;
}
int main()
{
int A[] = { 2, 4, 1, 9, 6 };
int B[] = { 8, 7, 3, 12, 7 };
int M = 0;
int N = sizeof (A) / sizeof (A[0]);
cout << maximumCandy(A, B, N, M);
return 0;
}
|
Java
public class GFG
{
static int maximumCandy( int []candies,
int []safety,
int N, int M)
{
int total = 0 ;
int ans = Integer.MAX_VALUE;
boolean all_safe = true ;
for ( int i = 0 ; i < N; i++)
{
if (candies[i] + M > safety[i])
{
all_safe = false ;
ans = Math.min(ans, safety[i]);
}
else
{
ans = Math.min(ans, candies[i] + M);
}
total += candies[i];
}
if (all_safe)
return total;
else
return ans;
}
public static void main (String[] args)
{
int A[] = { 4 , 5 , 2 , 3 };
int B[] = { 8 , 13 , 6 , 4 };
int M = 5 ;
int N = A.length;
System.out.println(maximumCandy(A, B, N, M));
}
}
|
Python3
def maximumCandy(candies, safety, N, M):
total = 0
ans = 10 * * 8
all_safe = True
for i in range (N):
if (candies[i] + M > safety[i]):
all_safe = False
ans = min (ans, safety[i])
else :
ans = min (ans, candies[i] + M)
total + = candies[i]
if (all_safe):
return total
else :
return ans
if __name__ = = '__main__' :
A = [ 4 , 5 , 2 , 3 ]
B = [ 8 , 13 , 6 , 4 ]
M = 5
N = len (A)
print (maximumCandy(A, B, N, M))
|
C#
using System;
class GFG {
static int maximumCandy( int [] candies, int [] safety, int N, int M)
{
int total = 0;
int ans = Int32.MaxValue;
bool all_safe = true ;
for ( int i = 0; i < N; i++) {
if (candies[i] + M > safety[i]) {
all_safe = false ;
ans = Math.Min(ans, safety[i]);
}
else {
ans = Math.Min(ans, candies[i] + M);
}
total += candies[i];
}
if (all_safe)
return total;
else
return ans;
}
static void Main()
{
int [] A = { 4, 5, 2, 3 };
int [] B = { 8, 13, 6, 4 };
int M = 5;
int N = A.Length;
Console.WriteLine(maximumCandy(A, B, N, M));
}
}
|
Javascript
<script>
function maximumCandy(candies, safety,
N, M)
{
let total = 0;
let ans = Number.MAX_VALUE;
let all_safe = true ;
for (let i = 0; i < N; i++)
{
if (candies[i] + M > safety[i])
{
all_safe = false ;
ans = Math.min(ans, safety[i]);
}
else
{
ans = Math.min(ans, candies[i] + M);
}
total += candies[i];
}
if (all_safe)
return total;
else
return ans;
}
let A = [ 4, 5, 2, 3 ];
let B = [ 8, 13, 6, 4 ];
let M = 5;
let N = A.length;
document.write(maximumCandy(A, B, N, M));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
09 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...