Given an array, find a pair such that their difference is maximum
For example:
If the given array is
-1 -2 -3 -4 5
then the maximum sum pair would be 5-(-4) = 9
Implementation:
For example:
If the given array is
-1 -2 -3 -4 5
then the maximum sum pair would be 5-(-4) = 9
Implementation:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* absolute function to get positive integer */
int abs(int n)
{
/* If the number is negative number, change the sign and return the number */
if(n<0)
return -(-n);
/* Else if the number is positive return the same number */
return n;
}
int main(void) {
int *a,i,n,maxdiff=INT_MIN,diff,j;
/* Get the number of elements of the array */
scanf("%d",&n);
/* Dynamically allocate the memory and store the starting address of the memory block in a */
a=(int*)malloc(sizeof(int)*n);
/* Get the elements of the array */
for(i=0;i<n;i++)
scanf("%d",(a+i));
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
/* find the difference between two elements of the array */
if(*(a+i)>*(a+j))
diff=*(a+i)-*(a+j);
else
diff=*(a+j)-*(a+i);
/* Store the maximum difference in maxdiff */
maxdiff=diff>maxdiff?diff:maxdiff;
}
}
/* Print the maximum difference between two numbers of the array */
printf("maximum difference of two numbers from the array is %d",maxdiff);
return 0;
}