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.