Problem in Test case

programming
Tags: #<Tag:0x00007f242ed11470>

#1

A : [ 2147483647, -2147483648, -2147483648, 0, 1 ]
B : 0
my output is: -2147483648
while expected output is: 0
can anyone explain why?

Code:
int ans;
int diff=INT_MAX;
int d;
for(int i=0;i<n;i++){
int j=i+1;
int k=n-1;
int target=B-A[i];
int sum=0;
while(j<k){
sum=A[j]+A[k];
cout<<sum<<" “<<target<<”\n";
if(sum<target){
j++;
}
if(sum>target){
k–;
}
d=abs(sum-target);
if(d<diff){
diff=d;
ans=sum+A[i];
}
if(sum==target)
break;
}
}


#2

The sum of the numbers is overflowing. Use long long instead wherever you are adding two numbers in the code.