Maximum difference between two subsets of m elements in C

The task is to find the maximum difference between the sum of two subsets, each containing m elements from an array. We need to find the subset with the highest sum and the subset with the lowest sum, then calculate their difference.

Syntax

int find_diff(int arr[], int length, int m);

Algorithm

The approach to solve this problem is −

  • Sort the array in ascending order
  • Sum the first m elements (lowest subset)
  • Sum the last m elements (highest subset)
  • Return the difference between highest and lowest sums

Example 1: Basic Implementation

Let's implement a complete solution with a sorting function −

#include <stdio.h>

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;
            }
        }
    }
}

int find_diff(int arr[], int length, int m) {
    sort(arr, length);
    int maxsum = 0, minsum = 0;
    
    for(int i = 0; i < m; i++) {
        minsum += arr[i];
        maxsum += arr[length - i - 1];
    }
    
    return (maxsum - minsum);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int m = 3;
    int length = 5;
    
    printf("Array: ");
    for(int i = 0; i < length; i++) {
        printf("%d ", arr[i]);
    }
    printf("\nValue of m: %d
", m); int result = find_diff(arr, length, m); printf("Maximum difference: %d
", result); return 0; }
Array: 1 2 3 4 5 
Value of m: 3
Maximum difference: 6

Example 2: Larger Array

Let's test with a larger array to verify our solution −

#include <stdio.h>

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;
            }
        }
    }
}

int find_diff(int arr[], int length, int m) {
    sort(arr, length);
    int maxsum = 0, minsum = 0;
    
    printf("After sorting: ");
    for(int i = 0; i < length; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); for(int i = 0; i < m; i++) { minsum += arr[i]; maxsum += arr[length - i - 1]; } printf("Lowest %d elements sum: %d
", m, minsum); printf("Highest %d elements sum: %d
", m, maxsum); return (maxsum - minsum); } int main() { int arr[] = {10, 13, 22, 8, 16, 14}; int m = 4; int length = 6; printf("Original array: "); for(int i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("
"); int result = find_diff(arr, length, m); printf("Maximum difference: %d
", result); return 0; }
Original array: 10 13 22 8 16 14 
After sorting: 8 10 13 14 16 22 
Lowest 4 elements sum: 45
Highest 4 elements sum: 65
Maximum difference: 20

Key Points

  • Time complexity is O(n log n) due to sorting
  • Space complexity is O(1) as we sort in-place
  • The array gets modified during sorting
  • Works for any valid value of m where m ? array length

Conclusion

The maximum difference between two subsets of m elements is efficiently calculated by sorting the array and finding the difference between the sum of the largest m elements and the smallest m elements.

Updated on: 2026-03-15T12:59:49+05:30

929 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements