SMPAIR - Editorial

That is not important in fact. If you get i > j, then you can swap the values of i and j so that i < j.

In 2 1 3 4 we have i = 1, j = 2. Here you probably think that you should have i < j AND a[i] < a[j] but there was no such constraint in the statement.

1 Like

But sorting needs O(nlogn) complexity.

your code fails for test case

1

3

4 8 2

Please explain what is your logic as it hard to understands others code!

Probably you have misunderstood the question. It is not that complicated as you think.

Since your code does not work for n=2 also, there must be some problem in your input. I dont think that the complex things you did to ‘save’ space was needed at all.

@yash_15 tell the mistake?

Can you please give me some test cases for which my code fails so that i could improve it. Thanks for helping me.

Hey!. Congrats your problem is solved you didn’t put \n in printf due to which you were getting wa. But now 3 cases are solved and only 1 is left. So keep trying…

My ans is accepted by making some modifications.Thank for ur help.

1 Like

please check what is wrong in the given code. online judge gave wrong answer for this
#include
#include
using namespace std;
int main()
{
int t,n,i,j;
cin>>t;
while(t–)
{
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
cout<<a[0]+a[1];

}
return 0;	

}

can u suggest me few test cases also

removing memset should work! You don’t need to initialize the array everytime.

ur code is okay …it will work if u initialise array with 100000 space i.e. a[100000]…i got the same problem…made 3 different codes…all failed due to same problem… i dont know what was that …it should happen though

actually that means both the elements neccesary choosen be at different index.

i can’t submit my code, it shows a Error massege like: it can not be submitted now. but my code run correctly. here is my code:
#include<bits/stdc++.h>

using namespace std;
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i+=1){
int n,min,a[10];
cin>>n;
for(int i=0;i<n;i+=1){
cin>>a[i];
}
min = a[0];
for(int i=0;i<n;i+=1){
if(min>a[i]){
min = a[i];
}
}
int secmin = 100000001;
for(int i=0;i<n;i+=1){
if(min<a[i] && secmin>a[i]){
secmin = a[i];
}
}
int s;
s = secmin+min;
cout<<s<<endl;
}

return 0;

}

The error may be due to the constraint of the array…if you read the problem again it says ‘a’ lies between 1 and 10^5… but you have taken a max array of a[10].
Therefore when the input is more than 10 numbers an error will occur.

https://www.codechef.com/viewsolution/30674222.
Please help.i think the algo is correct

I have a doubt if we sort than how can i < j will be maintained. since it is required in the question.

1 Like

yeah, if we sort then i<j cannot be maintained.
Acually the constrain is wrongly given.
It should be 1<=i,j<=N