#Problem link:
[Practice] CHEFDON Problem - CodeChef
[Codeyssey] CodeChef: Practical coding for everyone
Author: [Akshat Goyal] beast787 | CodeChef User Profile for Akshat Goyal | CodeChef
Tester: [V Surya Kumar] chief_surya01 | CodeChef User Profile for V Surya Kumar | CodeChef
Editorialist: [Akshat Goyal] beast787 | CodeChef User Profile for Akshat Goyal | CodeChef
DIFFICULTY: Cakewalk
# PREREQUISITES: Math
Problem with explanation:
Chef Wants to donate his apples in two cycles (i.e two times) each time he donates half of his apples so say he has n apples he donates n/2 but if he has odd number of apples he keeps the larger part and donates the smaller part. I.e If he has 5 apples he will donate 2 and keep 3.
He makes these donations twice so say he has 10 apples initially:
First cycle he donates n/2 which is 5 now he’s left with just 5 apples he again donates n/2 which in int will automatically take the lower value.
#Solutions:
Editorialist’s Solution
#include<bits/stdc++.h> //library of all libraries
using namespace std;
int main()
{
int t;
cin>>t; // taking input test cases
while(t- -) //for each test case remove space between - if want to run
{int n,m;
cin>>n;
m=n/2; //taking half apples if n=5 since n and m are both int m will become 2.
n=n-m; //subtracting from original number of apples(5-2=3)
m=n/2; //taking half apples if n=3 m will be 1 now.
n=n-m; //subtracting from original number of apples(3-1=2)
cout<<n<<endl; //Output n=2
}}
Setter’s Solution
t=int(input()) #input for test cases
for i in range(t): # for each test case
n=int(input()) #input initial apples
c=int(n/2)
#we half & type cast it to int so say n=5 n/2= 2.5 by int(2.5) it becomes 2
n=n-c #subtract from n the apples to be donated
c=int(n/2)
n=n-c
print(n)