 # FRUITS - Editorial

Author: Sunny Aggarwal
Tester: Vasya Antoniuk
Editorialist: Pushkar Mishra

Cakewalk

### PROBLEM:

Given are the number of apples (N) and oranges(M) and k, i.e., the amount of money that chef has to buy apples/oranges. Each orange/apple costs 1 unit. What is the minimum difference that can be achieved.

### EXPLANATION:

The problem is a very direct one. We have to minimize the difference. We can only increase the number of apples or oranges that we have. The logical step is to increase that commodity which is less because increasing the which is already more will only increase the the difference. Let minC denote the quantity which is less. Let maxC be the quantity which is more. The answer is given by:

ans = maxC - min(minC+k, maxC)

We have a min operation in the second term because the optimal case is to make both the quantities equal. If we have enough money to increase the lesser one to more than the other one, then we would rather make them equal so as to have a difference of zero, which is optimal.

### COMPLEXITY:

\mathcal{O}(1) per test case.

### SAMPLE SOLUTIONS:

1 Like

#include “iostream”
using namespace std;
int k;
void dif(int x,int y,int z);
void dif(int x,int y,int z)
{
int d;
if(x>y || x==y)
d=x-y;
else
d=y-x;
if(d<k || d==k)
{k=k-d;
if(k%2==0)
cout<<“0\n”;
else
cout<<“1\n”;}
else
{
if(x>y)
{y=y+k;
cout<<x-y<<"\n";
}
else
{x=x+k;
cout<<y-x<<"\n";
}
}
}

int main()
{
int t,m,n;
cin>>t;//no of test cases
do
{
cin>>m>>n>>k;//apple,oranges,money
dif(m,n,k);
t–;
}while(t!=0);
return 0;
}
//why is this giving a wrong answer

#include <stdio.h>
int same(int ,int ,int);
int fgreat(int ,int ,int);
int main(void) {

int t,n,m,k,result,i;
scanf("%d",&t);
printf("\n");
for(i=0;i<t;i++)
{
scanf("%d %d %d",&n,&m,&k);
if(n==m)
result[i] = same(n,m,k);
else if(n>m)
if(k<=(n-m))
{
m+=k;
result[i] = n-m;
}
else
result[i] = fgreat(n,m,k);
else
if(k<=(m-n))
{
n+=k;
result[i] = m-n;
}
else
result[i] = fgreat(m,n,k);
}

for(i=0;i<t;i++)
printf("\n%d",result[i]);

return 0;


}
int same(int n,int m,int k)
{
if(k%2==0)
{
n+=k/2;
m+=k/2;
}
else
{
n+=k/2;
m+=k-k/2;
}
return m-n;
}
int fgreat(int n,int m,int k)
{
k-=(n-m);
m+=n-m;
return same(n,m,k);
}

what is wrong in this code?

All 3 solution links are broken. Great. Don’t know why such a well known issue crops up each time.

https://www.codechef.com/viewsolution/10575699

What is wrong with my solution? Can anybody please help me cuz its supposed to be working fine for all test cases.

# include

using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,m,k;
cin>>n>>m>>k;
while(k–)
{
if(n<m)
n=n+1;
else if(m==n)
n=n+1;
else
m=m+1;
}
if(n>m)
cout<<n-m<<"\n";
else
cout<<m-n<<"\n";
}
return(0);
}

T=input()

C=0
while C<T :

N,M,K=map(int,raw_input().split(' '))
if N>M :
if (N-M)>K:
print N-(M+K)
elif (N-M)==K:
print 0
else:
if (K-(N-M))%2==0:
print 0
else:
print 1

else :
if (M-N)>K:
print M-(N+K)
elif (M-N)==K:
print 0
else:
if (K-(M-N))%2==0:
print 0
else:
print 1

C=C+1

#include<stdio.h>
int main()
{
int t1,t2,t=0,k=0,diff=0;
scanf("%d",&t);
while(t–)
{
scanf("%d%d%d",&t1,&t2,&k);
while(k>0&&(t1>0||t2>0))
{
if(t1>t2)
{
t1–;
}
else
{
t2–;
}
k–;

       }
if(t1>t2)
{
diff=t1-t2;
printf("%d\n",diff);
}
else{
diff=t2-t1;
printf("%d\n",diff);
}
}
return 0;
}


why it give wrong answer pledge help me

@laukesh_123

In your while loop you are checking if(t1>t2) or not.
That’s just two out of three possibilities…

1. t1>t2
2. t2>t1
3. t2=t1
The second and third conditions are in the “else” section of your code.
You should check for all three possibilities.

Can anyone explain me the condition 3 4 4 ? According to the formula the answer is coming as 0.
But the answer should come as 1.
Can anyone explain me this?

1 Like

Calculate the absolute difference between no. of apples and oranges. We can further minimize this difference by buying either of apple or oranges accordingly:

int diff = abs(n-m)-k;


Spend all our coins to buy fruits accordingly. Minimum absolute difference can be 0 only, so for negative answer output 0 i.e. dont spend more coins afterwards we’ve equal no. of fruits:

int ans = diff<0?0:diff;


https://www.codechef.com/viewsolution/15787068

#include<stdio.h>

#include<stdlib.h>

int main()

{

int t,n,m,k;

scanf("%d",&t);

while(t–)

{

scanf("%d %d %d",&n,&m,&k);

while(k–)

{

if(n<=m)

n += 1;

else

m += 1;


}

printf("%d\n",abs(m-n));

}

return 0;

}

This is my code. It resulted WA. Can you help me to find error ? condition in if : n is less than or equal to.

1 Like

/when i run this program on codeblock the output is correct it also satisfy the test case of this question but when i submit it it give the wrong answer what can i do please give some suggestion/
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int i,j,b,n,q;
cin>>q;
while(q–)
{
cin>>n;
int a;i=0;
a[i]=n;
for(i=1;i<n+1;i++)
cin>>a[i];b=1;
for(i=1;i<n;i++)
{
for(j=b>a[i]?b:a[i];a[i]%j || a[i+1]%j;j–);b=j;
}
for(i=1;i<n+1;i++)
{
a[i]=a[i]/b;
}
for(i=1;i<n+1;i++)
{
cout<<a[i]<<" ";
}

 }


}

The testcase 5 4 4 is not giving the right answer.
It is giving answer 0 but actually it should be 1. can anyone tell why> and if it is true edtior approach is wrong.

2 Likes

Please try to indent your code properly before submitting here if you want anyone to help you. I promise I will help you with this once you get this code all clearly indented. You can do so by just provide 4 times spaces before each line of your code.

Check this out: https://www.codechef.com/viewsolution/9223853

Fixed the formatting.

while(k>0&&(t1>0||t2>0))

{

if(t1>t2)

{

t1--;
}

else

{
t2--;
}
k--;

}


This part seems to be wrong. Lets say I got t1=t2 in between, then I should print 0. But your program would further reduce t2 and print 1.

#include
#include
using namespace std;

class Functions
{
public:
int getMinDifference(int apple, int orange, int coins)
{
int minDifference = 0;

		if(apple != orange)
{
if(apple > orange)
{
while(orange != apple)
{
coins = coins - 1;
orange = orange + 1;

if(coins == 0)
{
break;
}
}

if(coins != 0)
{
if(coins % 2 == 0)
{
apple = apple + (coins / 2);
orange = orange + (coins / 2);
}
else if(coins % 2 != 0)
{
apple = apple + ((coins / 2) + 1);
orange = orange + (coins / 2);
}
}

minDifference = apple - orange;
}
else if(apple < orange)
{
while(apple != orange)
{
coins = coins - 1;
apple = apple + 1;

if(coins == 0)
{
break;
}
}

if(coins != 0)
{
if(coins % 2 == 0)
{
orange = orange + (coins / 2);
apple = apple + (coins / 2);
}
else if(coins % 2 != 0)
{
orange = orange + ((coins / 2) + 1);
apple = apple + (coins / 2);
}
}

minDifference = orange - apple;
}
}
else if(apple == orange)
{
if(coins % 2 == 0)
{
apple = apple + (coins / 2);
orange = orange + (coins / 2);

minDifference = apple - orange;
}
else if(coins % 2 != 0)
{
apple = apple + ((coins / 2) + 1);
orange = orange + (coins / 2);

minDifference = apple - orange;
}
}

return minDifference;
}


};

int main()
{
Functions functions;

int testCase;
int apple, orange, coins;

cin >> testCase;

if((1 <= testCase) && (testCase <= 100))
{
for(int i = 0; i < testCase; i++)
{
cin >> apple >> orange >> coins;

if((1 <= apple) && (apple <= 100) && (1 <= orange) && (orange <= 100) && (1 <= coins) && (coins <= 100))
{
cout << functions.getMinDifference(apple, orange, coins) << endl;
}
}
}

return 0;


}

1 Like