PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: jeevanjyot
Testers: iceknight1093, tabr
Editorialist: kiran8268
DIFFICULTY:
702
PREREQUISITES:
None
PROBLEM:
Chef’s phone has a total storage of S MB, of which X MB and Y MB of spaces are acquired by 2 apps which are already installed. Chef wants to add a new app with a memory requirement of Z MB. The objective is to find the minimum number apps should be removed so that the new app can be installed
EXPLANATION:
-
What is the available free memory when there are 2 apps already installed?
Free memory=S-(X+Y) -
Lets compare the free memory with the additional required memory Z.
a. Additional memory required, say A = Z- {S-(X+Y)} -
Solutions
a. If A is less than or equal to Z, output 0
b. If A is less than or equal to max of X and Y, output 1
c. If both a and b are not true then output 2
TIME COMPLEXITY:
Time complexity is O(1).
SOLUTION:
Editorialist's Solution
int t;
cin>>t;
for(int k=0; k<t;k++)
{
int s,x,y,z,a;
cin>>s>>x>>y>>z;
a=z-(s-(x+y));
if(a<=0)
cout<<"0"<<"\n";
else if(a<=max(x,y))
cout<<"1"<<"\n";
else
cout<<"2"<<"\n";
}
return 0;
}