CHEFAPPS-Editorial

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:

  1. What is the available free memory when there are 2 apps already installed?
    Free memory=S-(X+Y)

  2. Lets compare the free memory with the additional required memory Z.
    a. Additional memory required, say A = Z- {S-(X+Y)}

  3. 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;
}

   else if(a<=max(x,y))
   cout<<"1"<<"\n";

In this line of the code, what if we remove the min(x, y), it would still make sense right?

No, min(X,Y ) will not work.
Consider the case, If the value of a is greater than X but less than Y. Now if you take the min(X,Y), the if will return false because min(X,Y) is X and a > X