#include <stdio.h>

#include <stdlib.h>

/* Comparator function */

int intCompare(int *a, int *b)

{

return *a - *b;

}

int maxsum(int a[], int n)

{

/* Sort the array */

qsort(a, n, sizeof(int), intCompare);

int i, j;

while(n--)

{

i = 0;

j = n - 1;

/* Check whether 0 <= i <= n-1, 0 <= j <= n-1 exists such that a[i] + a[j] = a[n] */

while(i < j)

{

int sum = a[i] + a[j];

if(sum == a[n])

return sum;

if(sum > a[n])

j--;

else

i++;

}

}

/* Can't find any such num */

return -1;

}

### Like this:

Like Loading...

*Related*