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.
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…
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];
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
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;
}
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.