Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum difference between the group of k-elements and rest of the array in C
We are given 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 k-elements) - (sum of rest N-k elements).
Syntax
int maxKDiff(int arr[], int n, int k);
Example 1: k is smaller than half of array size
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
Example 2: k is larger than half of array size
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 = 25
Implementation
The approach is to sort the array first and then calculate the difference for both cases −
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// Function to sort array in ascending order
void sort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
// 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 < k; 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<br>", maxKDiff(arr, n, k));
return 0;
}
Maximum difference between the group of k-elements and rest of the array: 30
How It Works
- First, sort the array in ascending order to easily identify smallest and largest k elements.
- Calculate the sum of the whole array.
- Find sum of smallest k elements (first k elements after sorting).
- Calculate difference D1 using formula: |arrsum - 2 * sumk| where sumk is sum of smallest k elements.
- Find sum of largest k elements (last k elements after sorting).
- Calculate difference D2 using formula: |arrsum - 2 * sumk| where sumk is sum of largest k elements.
- Return the maximum of D1 and D2.
Conclusion
The algorithm efficiently finds the maximum difference by comparing both possible cases: selecting k smallest elements versus k largest elements. The time complexity is O(n²) due to sorting, and space complexity is O(1).
