×

# 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:

This question is marked "community wiki".

1.3k156381
accept rate: 4%

19.0k348495531

# 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

2★reshabh
1
accept rate: 0%

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.

(03 Feb '16, 02:01)

# include <stdio.h>

int same(int ,int ,int); int fgreat(int ,int ,int); int main(void) {

int t,n,m,k,result[10],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?

0★smd_1996
1
accept rate: 0%

 0 All 3 solution links are broken. Great. :( Don't know why such a well known issue crops up each time. answered 29 Feb '16, 02:11 3★esemzv 21●2 accept rate: 0% Check this out: https://www.codechef.com/viewsolution/9223853 (29 Feb '16, 05:07)
 0 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. answered 22 Jun '16, 17:42 1 accept rate: 0%

# include<iostream>

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&lt;&lt;"\n"; else="" cout&lt;<m-n&lt;&lt;"\n";="" }="" return(0);="" }<="" p="">

This answer is marked "community wiki".

1★ny826
1
accept rate: 0%

(27 Jun '16, 18:57) 1★
 0 T=input() C=0 while CM : 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  answered 13 Sep '16, 18:23 0★aashu771 1 accept rate: 0%
 0 #include 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 answered 23 Apr '17, 00:41 1 accept rate: 0% 13.6k●1●10●36 Fixed the formatting. (23 Apr '17, 01:40) 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. (23 Apr '17, 01:42)
 0 @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. answered 23 Apr '17, 09:35 31●2 accept rate: 0%
 0 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? answered 19 Sep '17, 20:38 31●1 accept rate: 0%
 0 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;  Solution Link: https://www.codechef.com/viewsolution/15787068 answered 12 Oct '17, 12:13 1 accept rate: 0%

# 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
accept rate: 0%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×14,366
×1,410
×854
×54
×3

question asked: 21 Jan '16, 18:56

question was seen: 4,653 times

last updated: 16 Mar, 17:45