Let A be a non-empty array consisting of N integers.
The abs sum of two for a pair of indices (P, Q) is the absolute value |A[P] + A[Q]|, for 0 ≤ P ≤ Q < N.
For example, the following array A:
A[0] = 1 A[1] = 4 A[2] = -3has pairs of indices (0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2).
The abs sum of two for the pair (0, 0) is A[0] + A[0] = |1 + 1| = 2.
The abs sum of two for the pair (0, 1) is A[0] + A[1] = |1 + 4| = 5.
The abs sum of two for the pair (0, 2) is A[0] + A[2] = |1 + (−3)| = 2.
The abs sum of two for the pair (1, 1) is A[1] + A[1] = |4 + 4| = 8.
The abs sum of two for the pair (1, 2) is A[1] + A[2] = |4 + (−3)| = 1.
The abs sum of two for the pair (2, 2) is A[2] + A[2] = |(−3) + (−3)| = 6.
Write a function:
int solution(int A[], int N);
that, given a non-empty array A consisting of N integers, returns the minimal abs sum of two for any pair of indices in this array.
For example, given the following array A:
A[0] = 1 A[1] = 4 A[2] = -3the function should return 1, as explained above.
Given array A:
A[0] = -8 A[1] = 4 A[2] = 5 A[3] =-10 A[4] = 3the function should return |(−8) + 5| = 3.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
int diff=abs(A[0]+A[0]);
if(A[0]==A[N-1]) return diff;
for(int i=0;i<N;i++){
int signsrc=(A[i]<0)?-1:1;
for(int j=i;j<N;j++){
int signdst=(A[j]<0)?-1:1;
if(signsrc!=signdst){
if(diff<=abs(A[i]+A[j])) break;
diff=abs(A[i]+A[j]);
//printf("diff[%d]:A[%d](%d)+A[%d](%d)\n",diff,i,A[i],j,A[j]);
if(diff==0) return 0;
}
}
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0)
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[0]+A[0]);
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[N-1]+A[N-1]);
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
for(int i=0;i<N;i++)
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
for(int i=0;i<N;i++){
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
for(int i=0;i<N;i++){
if(A[z]<0)
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
for(int i=0;i<N;i++){
if(A[z]<0)z++;
else if(A[z]>0)z--;
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z-i])
}
else{
}
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
}
else{
}
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
}
else{
if(A[z-i]<0) break;
}
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
while(min<max){
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
while(min<max){
int
}
return diff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
while(min<max){
int
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=
while(min<max){
int
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0)
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
p
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
printf("[%d]:A[%d](%d) + A[%d](%d)\n",abssum,min,A[min])
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
printf("[%d]:A[%d](%d) + A[%d](%d)\n",abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",,abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
[1][1]:A[0](-3) + A[1](1) [1][2]:A[0](-3) + A[0](-3)
[5][5]:A[0](-10) + A[3](4) [5][6]:A[0](-10) + A[2](3) [5][7]:A[0](-10) + A[1](-8) [5][18]:A[0](-10) + A[0](-10)
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=(A[min]+A[max]);
int abssum=abs(A[min]+A[max]);
if(abssum<0) min++;
else if(abssum>0) max--;
else return 0;
if(minsum>abssum)minsum=abssum;
printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=(A[min]+A[max]);
if(sum<0) min++;
else if(sum>0) max--;
else return 0;
int abssum=abs(sum);
if(minsum>abssum)minsum=abssum;
printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=(A[min]+A[max]);
if(sum<0) min++;
else if(sum>0) max--;
else return 0;
int abssum=abs(sum);
if(minsum>abssum)minsum=abssum;
printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
[1][1]:A[0](-3) + A[1](1) [1][2]:A[1](1) + A[1](1)
[5][5]:A[1](-8) + A[4](5) [3][3]:A[2](3) + A[4](5) [3][8]:A[2](3) + A[3](4) [3][7]:A[2](3) + A[2](3)
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=(A[min]+A[max]);
if(sum<0) min++;
else if(sum>0) max--;
else return 0;
int abssum=abs(sum);
if(minsum>abssum)minsum=abssum;
//printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=(A[min]+A[max]);
if(sum<0) min++;
else if(sum>0) max--;
else return 0;
int abssum=abs(sum);
if(minsum>abssum)minsum=abssum;
//printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int compare(const void*a,const void*b){
int A=(*(int*)a);
int B=(*(int*)b);
return (A>B)?1:(A<B)?-1:0;
}
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
qsort(A,N,sizeof(int),compare);
if(A[0]<0&&A[N-1]<0) return abs(A[N-1]+A[N-1]);
if(A[0]>0&&A[N-1]>0) return abs(A[0]+A[0]);
//find 0
int z=N/2;
int dir=A[z]<0?-1:1;
for(int i=0;i<N;i++){
if(dir==-1){
if(A[z+i]>0) break;
if(A[z+i]==0) return 0;
}
else{
if(A[z-i]<0) break;
if(A[z-i]==0) return 0;
}
}
int min=0;
int max=N-1;
int minsum=abs(A[0]+A[N-1]);
while(min<max){
int sum=(A[min]+A[max]);
if(sum<0) min++;
else if(sum>0) max--;
else return 0;
int abssum=abs(sum);
if(minsum>abssum)minsum=abssum;
//printf("[%d][%d]:A[%d](%d) + A[%d](%d)\n",minsum,abssum,min,A[min],max,A[max]);
}
return minsum;
}
The solution obtained perfect score.