Problem Link:
Author: Misha Chorniy
Tester: Pushkar Mishra
Editorialist: Praveen Dhinwa
Difficulty:
Cakewalk
Pre-Requisites:
Implementation, Basic maths.
Problem Statement
Chef has money rupees with him. He spends jacketCost rupees for buying a jacket. He buys as many socks as he can by the money left after purchasing the jacket. Cost of a sock is given by sockCost. Each day, he uses two socks. He never cleans his socks after using them. You have to find whether there will be a day in which Chef will have one sock remaining.
Quick Explanation
We just have to check whether \frac{(money - jacketCost)}{sockCost} is even or odd.
Explanation
Initial money that Chef have = money
We are guaranteed in the problem that jacketCost \leq money.
So the money left after buying a jacket (i.e. money - jacketCost) will be always non-negative.
Using the remaining money, Chef buys as many socks as we can, so he will buy S socks where S = \frac{(money - jacketCost)}{sockCost} where / denotes an integer division. By integer division, we mean floor function (e.g. \lfloor \frac{3}{2} \rfloor = 1 and \lfloor \frac{4}{2} \rfloor = 2).
Now, we need to check if Chef wear two socks daily, will there be a day when he will have to wear a single sock? This is equivalent to checking S is even or not.
For solving subtask 1, we can simply simulate the process of wearing two socks as follows:
int remS = S // remS denotes initial number of socks.
while (remS >= 2) {
// wear two socks
remS -= 2;
}
if (remS == 1) return "Unlucky Chef"
else return "Lucky Chef";
Time Complexity
O(S) or O(1) depending on the subtask.
Solution:
Setter’s solution can be found here
Tester’s solution can be found here
Please feel free to post comments if anything is not clear to you.