# Maximum difference between the group of k-elements and rest of the array in C

CServer Side ProgrammingProgramming

We are given with an array of integers of size N and a number k. The array consists of integers in random order. The task is to find the maximum difference between the group of k-elements and rest of the array. The array will be divided into two parts. The first part is a group of k-elements taken out and the second part is the rest of the elements of the array. We have to select k elements such that the difference between the sum of elements in both groups is maximum.

If k is smaller (<= half of array size) then smallest k elements will have least sum and rest N-k elements will have largest sum. So the maximum difference is − (sum of rest N-k elements) - (sum of smallest k-elements).

If k is larger (> half of array size) then largest k elements will have the greatest sum and rest N-k elements will have least sum. So the maximum difference is (sum of largest kelements) - (sum of rest N-k elements).

Input

Arr[] = { 2,5,6,1,3,2,1,4 }. k=3

Output − Maximum difference between the group of k-elements and rest of the array − 16

Explanation − Here k is smaller so the least 3 numbers would have the smallest sum.

Least 3 numbers − 1,1,2 sum=4

Rest N-k = 5 numbers: 2,3,4,5,6 sum=20

Maximum difference : 20-4 = 16

Input

Arr[] = { 2,2,3,4,8,3,4,4,8,7 }. k=6

Output − Maximum difference between the group of k-elements and rest of the array − 25

Explanation − Here k is larger so the highest 6 numbers would have the largest sum.

Highest 6 numbers − 8,8,7,4,4,4, sum=35

Rest N-k = 4 numbers − 2,2,3,3 sum=10

Maximum difference − 35-10=

## Approach used in the below program is as follows

• Declare an array of integers which contains in random order.( Arr[] )

• Create a variable to store the size of the array. (N)

• The function maxKDiff( int Arr[],int n, int k) is used to calculate the maximum difference (maxD) between first and last indexes of an element in an array.

• Calculate the sum of whole array and store in arrsum.

• First thing is to calculate the sum of least k elements. Using for loop ( i=0;i<k)

If k is smaller then the smallest k elements would have least sum −

• In D1 store the abs((sum of whole array) - (2*sum of least k elements)). Twice because array sum also has these elements.

k is larger then the largest k elements would have highest sum −

• In D2 store the abs((sum of whole array) - (2*sum of highest k elements)). Twice because array sum also has these elements.

• Compare D1 with D2 and store maximum value in maxD.

• Return maxD as the result.

## Example

#include <stdio.h>
#include <math.h>
// function for finding maximum group difference of array
int maxKDiff (int arr[], int n, int k){
// sum of array
int arrsum = 0;
int i;
for(i=0;i<n;i++)
arrsum+=arr[i];
//sum of smallest k
int sumk=0;
for(i=0;i<k;i++)
sumk+=arr[i];
// difference for k-smallest
int D1 = abs(arrsum - 2*sumk);
//sum of largest k elements
sumk=0;
int j=0;
for(i=n-1;j<4;i--){
sumk+=arr[i]; j++;
}
// difference for k-largest
int D2 = abs(arrsum - 2*sumk);
int maxD=D1>=D2?D1:D2;
// return maximum difference value
return maxD;
}
// driver program
int main(){
int arr[] ={ 2,3,2,10,7,12,8};
int n = 7;
int k = 3;
sort(arr,n); // to sort array in ascending order
printf("Maximum difference between the group of k-elements and rest of the array : %d" , maxKDiff(arr,n,k));
return 0;
}

## Output

If we run the above code it will generate the following output −

Maximum difference between the group of k-elements and rest of the array : 30