PROBLEM LINK:
Practice : TOFFIES IN JAR
Author: RAKSHIT NAYAK
Tester: RAKSHIT NAYAK
Editorialist: RAKSHIT NAYAK
DIFFICULTY:
CAKE WALK
PREREQUISITES:
Basic Math
PROBLEM:
There is a JAR full of toffies for sale at a mall counter. JAR has the capacity N, that is JAR can contain maximum N toffies when JAR is full. At any point of time. JAR can have M number of Candies where M<=N. toffies are served to the customers. JAR never remains empty as when last k toffies are left. JAR is refilled with new candies in such a way that JAR gets full.
Write a code to implement above scenario. Display JAR at counter with available number of toffies.
Quick Explanation:
Hope you have gone through the problem statement and i/o, So there is jar which contains N number of candies when it is full. So when customer buys some candies the jar can ave M number of candies in it,where M should be less or equal to 10 .
- ex: If N=10 ,M cannot be more than 10, i.e M=11,12 …is invalid.it should be less than or 10.
And note that jar should never be empty ,there should be some candies left .In our case k=5 is the minimum number of candies that must be inside the jar ever.
Approach:
- If (num>=1 && num<=5), therefore, we can give the customer candies and return the remaining candies in the jar by subtracting N from num
- else if num>5 ,i.e 6,7,8,9,10 we will print output as invalid input,because there must be minimum 5 candies left in jar. if customer buys 6 candies there will be 4 candies in left in jar which is inavlid .
SOLUTION:
Setter's Solution
#include <iostream>
using namespace std;
int main()
{
int n=10, k=5;
int num;
cin>>num;
if(num>=1 && num<=5)
{
cout<<"NUMBER OF toffies SOLD : "<<num<<"\n";
cout<<"NUMBER OF toffies AVAILABLE : "<<n-num;
}
else
{
cout<<"Invalid input"<<"\n";
cout<<"NUMBER OF toffies AVAILABLE : "<<n;
}
return 0;
}
Well this was my approach, feel free to share your approach here. Suggestions are always welcomed.