The problem statement says, find smallest a[i]+a[j] such that 1 <= i < j <= n. If we sort the array, won’t that disturb the order? If this algo is giving AC, then the problem statement shold be changed to 1 <= i,j <= N
Yeah it is too easy for me as i have written it for 13pts(and i have achieved it) its just the sum of those 2 input value here is my code…
program SMPAIR;
var
T,num,sum:longint;
N:smallint;
begin
(* Solution to SMPAIR *)
readln(T);
while T <> 0 do
begin
readln(N);
sum := 0;
while N <> 0 do
begin
read(num);
sum := sum + num;
dec(N);
end;
writeln(sum);
dec(T);
end;
end.
Only using printf and cout made a difference , it failed last subtask with cout but passed with printf , same code
Also, I wasted a hour finding the bug which was just the absence of newline character 
i m not getting this constraint concept… plzz explain one constriant in brief so thati can code
#include
using namespace std;
int main()
{long long t,n,i,min;
cin>>t;
while(t–)
{ cin>>n;
long long A[n];
for(i=0;i<n;i++)
{cin>>A[i];
}
min=A[0]+A[1];
for(i=0;i<n;i++)
{ if((A[i]+A[i+1])<=min)
min=A[i]+A[i+1];
}
cout<<min<<endl;
}
return 0;
}
wats d problem in my code???
what is wrong in my code…its showing wrong answer
#include
using namespace std;
int main()
{
int t,n,y,largest,smallest,secondSmallest;
cin>>t;
while(t–)
{
cin>>n;
int a[n];
y=0;
while(y<n)
{
cin>>a[y];
y++;
}
largest=a[0];
smallest=a[0];
y=1;
while(y<=n)
{
if(a[y]>largest) largest=a[y];
if(a[y]<smallest) smallest=a[y];
y++;
}
if(smallest!=largest)
{
secondSmallest=largest;
y=0;
while(y<=n)
{
if(a[y]<secondSmallest && a[y]!=smallest) secondSmallest=a[y];
y++;
}
}
cout<<smallest+secondSmallest<<endl;
}
}
hey i get the result as wrong answer in codechef for my code but while executing the code in my compiler it runs good and gives correct answer
using namespace std;
int main()
{int t,n,a[10],sum,j,i,temp=1000;
cin>>t;
while(t–)
{cin>>n;
for(i=0;i<n;i++)
{cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
sum=a[i]+a[j];
if(sum<temp)
temp=sum;
}
}
cout<<temp<<endl;
}
return 0;
}
whats wrong i it?
#include
using namespace std;
int main()
{
long long t,n,sum=0,i,min=1000000;
long long a[1000];
cin>>t;
int j=0;
while(j<t)
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
sum=a[i]+a[i+1];
if(sum<min)min=sum;
}
cout<<min<<endl;
j++;
}
return 0;
}
why i am getting WA??Can anyone help??
but you are essentially finding the smallest and second smallest element.
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.
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.
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.