FRUITS - Editorial

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?

2 Likes

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:
CodeChef: Practical coding for everyone

1 Like

#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[100];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: CodeChef: Practical coding for everyone

why my answer is wrong

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.

please tell me why this is causing an incorrect answer.

#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

i also did the same Still confused why its Wrong

Whats wrong with my code i don’t understand. Its giving me a wrong answer !! Can someone help me ?

#include <iostream>

using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
    int n,m,k ;
    cin>>n>>m>>k;
    int i=0 ; // here i is the count of coins 
    while(true)
    {
        if(n<m)
            n+=1 ;
        else
            m+=1 ;
        i++ ;
        if(i==k)   // if number of coins are k then terminate
            break ;
    }
    cout<<abs(n-m)<<"\n";
}
return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
int T;
cin>>T;
int n,m,k,d;
for(int i=0;i<T;i++){
cin>>n>>m>>k;
d = abs(n-m) - k;
if(d<0){
cout<<(-d)%2<<endl;
}else{
cout<<d<<endl;
}
}

return 0;

}

why is this wrong??

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a,i,o,c;
cin>>a>>o>>c;
if(a==o)
{
if(c%2==0)

         cout<<"0"<<endl;

    else
        cout<<"1"<<endl;
    }

else
    {
         i=abs(a-o);
    if(i>=c)
        cout<<i-c<<endl;
    else
        if(c%2==0)
            cout<<"1"<<endl;
        else
            cout<<"0"<<endl;


    }

}
return 0;
}
Why is it wrong ?

DIid you got the reason ?

#include
using namespace std;

int main() {
int x,n=0,m=0,k=0;
cin>>x;
for(int i=0;i<x;i++){
cin>>n>>m>>k;
for(int j=0;j<k;j++){
if(n>m){
m=m+1;
}
else{
n=n+1;
}
}
if(n>m){
cout<<n-m<<endl;}
else{cout<<m-n<<endl;}

}
return 0;
}

Why wrong? please help