You are not logged in. Please login at www.codechef.com to post your questions!

×

FRUITS - Editorial

PROBLEM LINK:

Practice
Contest

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

DIFFICULTY:

Cakewalk

PREREQUISITES:

Ad-hoc

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:

Author
Tester
Editorialist

This question is marked "community wiki".

asked 21 Jan '16, 18:56

pushkarmishra's gravatar image

4★pushkarmishra
1.3k156281
accept rate: 4%

edited 10 Mar '16, 13:20

admin's gravatar image

0★admin ♦♦
17.4k347486515


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

link

answered 27 Jan '16, 23:30

reshabh's gravatar image

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) shubham992★

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?

link

answered 05 Feb '16, 03:09

smd_1996's gravatar image

0★smd_1996
1
accept rate: 0%

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

link

answered 29 Feb '16, 02:11

esemzv's gravatar image

3★esemzv
212
accept rate: 0%

(29 Feb '16, 05:07) anmol137dh085★

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.

link

answered 22 Jun '16, 17:42

akshelmerith's gravatar image

0★akshelmerith
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="">

link
This answer is marked "community wiki".

answered 27 Jun '16, 18:56

ny826's gravatar image

1★ny826
1
accept rate: 0%

why my answer is wrong

(27 Jun '16, 18:57) ny8261★

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
link

answered 13 Sep '16, 18:23

aashu771's gravatar image

0★aashu771
1
accept rate: 0%

#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

link

answered 23 Apr, 00:41

laukesh_123's gravatar image

0★laukesh_123
1
accept rate: 0%

edited 23 Apr, 01:40

vijju123's gravatar image

4★vijju123 ♦
11.3k1316

Fixed the formatting.

(23 Apr, 01:40) vijju123 ♦4★

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, 01:42) vijju123 ♦4★

@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.

link

answered 23 Apr, 09:35

goof_expert's gravatar image

3★goof_expert
312
accept rate: 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?

link

answered 19 Sep, 20:38

write2diba's gravatar image

2★write2diba
311
accept rate: 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

link

answered 12 Oct, 12:13

the_baahubali's gravatar image

3★the_baahubali
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:

×12,235
×1,147
×787
×53
×3

question asked: 21 Jan '16, 18:56

question was seen: 4,165 times

last updated: 12 Oct, 12:13