Open In App

Maximum difference between two subsets of m elements

Last Updated : 03 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of n integers and a number m, find the maximum possible difference between two sets of m elements chosen from given array.

Examples: 

Input : arr[] = 1 2 3 4 5
            m = 4
Output : 4
The maximum four elements are 2, 3, 
4 and 5. The minimum four elements are 
1, 2, 3 and 4. The difference between
two sums is (2 + 3 + 4 + 5) - (1 + 2
+ 3 + 4) = 4
  
Input : arr[] = 5 8 11 40 15
           m = 2
Output : 42
The difference is (40 + 15) - (5  + 8)           

The idea is to first sort the array, then find sum of first m elements and sum of last m elements. Finally return difference between two sums.

Implementation:

CPP




// C++ program to find difference
// between max and min sum of array
#include <bits/stdc++.h>
using namespace std;
 
// utility function
int find_difference(int arr[], int n, int m)
{
    int max = 0, min = 0;
 
    // sort array
    sort(arr, arr + n);
 
    for (int i = 0, j = n - 1;
         i < m; i++, j--) {
        min += arr[i];
        max += arr[j];
    }
 
    return (max - min);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 4;
    cout << find_difference(arr, n, m);
    return 0;
}


Java




// Java program to find difference
// between max and min sum of array
import java.util.Arrays;
 
class GFG {
    // utility function
    static int find_difference(int arr[], int n,
                               int m)
    {
        int max = 0, min = 0;
 
        // sort array
        Arrays.sort(arr);
 
        for (int i = 0, j = n - 1;
             i < m; i++, j--) {
            min += arr[i];
            max += arr[j];
        }
 
        return (max - min);
    }
 
    // Driver program
    public static void main(String arg[])
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        int n = arr.length;
        int m = 4;
        System.out.print(find_difference(arr, n, m));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python program to
# find difference
# between max and
# min sum of array
 
def find_difference(arr, n, m):
    max = 0; min = 0
      
    # sort array
    arr.sort();
    j = n-1
    for i in range(m):
        min += arr[i]
        max += arr[j]
        j = j - 1
      
    return (max - min)
  
# Driver code
if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    n = len(arr)
    m = 4
 
    print(find_difference(arr, n, m))  
 
# This code is contributed by
# Harshit Saini


C#




// C# program to find difference
// between max and min sum of array
using System;
 
class GFG {
     
    // utility function
    static int find_difference(int[] arr, int n,
                                          int m)
    {
        int max = 0, min = 0;
 
        // sort array
        Array.Sort(arr);
 
        for (int i = 0, j = n - 1;
            i < m; i++, j--) {
            min += arr[i];
            max += arr[j];
        }
 
        return (max - min);
    }
 
    // Driver program
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        int m = 4;
        Console.Write(find_difference(arr, n, m));
    }
}
 
// This code is contributed by nitin mittal


PHP




<?php
// PHP program to find difference
// between max and min sum of array
 
// utility function
function find_difference($arr, $n, $m)
{
    $max = 0; $min = 0;
 
    // sort array
    sort($arr);
    sort( $arr,$n);
 
    for($i = 0, $j = $n - 1; $i <$m; $i++, $j--)
    {
        $min += $arr[$i];
        $max += $arr[$j];
    }
 
    return ($max - $min);
}
 
// Driver code
{
    $arr = array(1, 2, 3, 4, 5);
    $n = sizeof($arr) / sizeof($arr[0]);
    $m = 4;
    echo find_difference($arr, $n, $m);
    return 0;
}
 
// This code is contributed by nitin mittal.
?>


Javascript




<script>
// JavaScript program to find difference
// between max and min sum of array
 
// utility function
    function find_difference(arr, n,
                               m)
    {
        let max = 0, min = 0;
   
        // sort array
        arr.sort();
   
        for (let i = 0, j = n - 1;
             i < m; i++, j--) {
            min += arr[i];
            max += arr[j];
        }
   
        return (max - min);
    }
 
// Driver Code
 
        let arr = [ 1, 2, 3, 4, 5 ];
        let n = arr.length;
        let m = 4;
        document.write(find_difference(arr, n, m));
         
        // This code is contributed by splevel62.
</script>


Output

4

Time Complexity : O(n Log n) 
Auxiliary Space : O(1)

We can optimize the above solution using more efficient approaches discussed in below post. 
k largest(or smallest) elements in an array | added Min Heap method



Previous Article
Next Article

Similar Reads

Divide array in two Subsets such that sum of square of sum of both subsets is maximum
Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pairs are as follows (1)2 + (2 + 3)2 = 26 (2)2 + (1
6 min read
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K
Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2, 9, 5}, K = 10Output: 2Explanation:All such disjoi
8 min read
Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
Given an array of size N. The task is to partition the given array into two subsets such that the average of all the elements in both subsets is equal. If no such partition exists print -1. Otherwise, print the partitions. If multiple solutions exist, print the solution where the length of the first subset is minimum. If there is still a tie then p
14 min read
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once
Given an array arr[] consisting of N distinct integers, the task is to find the minimum number of times the array needs to be split into two subsets such that elements of each pair are present into two different subsets at least once. Examples: Input: arr[] = { 3, 4, 2, 1, 5 } Output: 3 Explanation: Possible pairs are { (1, 2), (1, 3), (1, 4), (1,
6 min read
Maximum Partitions such that any two numbers from two Subsets are co-prime
Given an array arr[] of length N, the task is to find the maximum number of subsets that can be obtained from the given array such that the GCD of every a and b that belong to two different subsets is 1. Example: Input: arr[] = {2, 3, 4, 5, 6}Output: 2?Explanation: Divide array into 2 subset such as S1 = { 2, 3, 4, 6 }, S2= { 5 }, gcd for every a a
10 min read
Sum of subsets of all the subsets of an array | O(3^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Sum of subsets of all the subsets of an array | O(2^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Sum of subsets of all the subsets of an array | O(N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
7 min read
Maximum sum of Bitwise XOR of all elements of two equal length subsets
Given an array arr[] of N integers, where N is an even number. The task is to divide the given N integers into two equal subsets such that the sum of Bitwise XOR of all elements of two subsets is maximum. Examples: Input: N= 4, arr[] = {1, 2, 3, 4} Output: 10 Explanation:There are 3 ways possible: (1, 2)(3, 4) = (1^2)+(3^4) = 10(1, 3)(2, 4) = (1^3)
14 min read
Partition into two subsets of lengths K and (N - k) such that the difference of sums is maximum
Given an array of non-negative integers of length N and an integer K. Partition the given array into two subsets of length K and N - K so that the difference between the sum of both subsets is maximum. Examples : Input : arr[] = {8, 4, 5, 2, 10} k = 2 Output : 17 Explanation : Here, we can make first subset of length k = {4, 2} and second subset of
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg