# Why my code is not accepting even it is correct solution for the problem

### My code

``````#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int a,b,c,n=3;
int arr[n], max=0, max2=0;

for(int i=0;i<n;i++)
{
cin>>arr[i];
}

for(int i=0;i<n;i++)
{
if(arr[i] >= max)
{
max2 = max;
max = arr[i];
}
}
cout<<max+max2<<endl;

}
return 0;
}

``````

Problem Link: CHFSPL Problem - CodeChef

@ayushagrawal07
In your code, the code block used below will only correctly worked for an array sorted in ascending order, but in the problem statement we might or might not get pre-arranged values.

Consider the values being (1, 2, 3). Here, after final iteration, we will get (max=3) and (max2=2) and the answer will be correct.

But if the values were like, (3, 1, 2). Here,

• After first iteration, we get (max=3) and (max2=0).
• After second iteration, (max=3) and (max2=0), as A[i] will be lesser than max.
*Same happens in third iteration.

Finally we have, (max=3) and (max2=0), which will be incorrect.

You could either sort the array before finding max and max2 and the code should work fine.

``````for(int i=0;i<n;i++)
{
if(arr[i] >= max)
{
max2 = max;
max = arr[i];
}
}
``````

The logic I used to solve the problem was like this.

We are provided with three number and have to find the sum of largest two from them.

The problem can also be changed as finding the sum of all three and subtracting the smallest of three.

Here is my code for reference.

``````# cook your dish here
for _ in range(int(input())):
a,b,c=map(int,input().split())
print((a+b+c)-min(a,b,c))

``````
1 Like

Yes, you are correct.
Thanks for finding my error and responding as well as explaining it to me.