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.
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
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.
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?
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:
/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.