
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Maximum difference between two subsets of m elements in C
The task is to find the greatest difference between the sum of m elements in an array. Suppose we have an array and a number m, then we will first find the sum of highest m numbers and then subtract the sum of lowest m numbers from it to get the maximum difference. So the main thing is to find two subsets of m numbers which have the highest sum and lowest sum.
Let’s now understand what we have to do using an example −
Input
arr = {1,2,3,4,5} ; m=3
Output
Maximum difference here is : 6
Explanation − Here the highest 3 numbers are 3,4,5 and the sum is 12. Lowest 3 numbers are 1,2,3 and sum is 6. So the highest or maximum difference is 12-6 which is 6.
Input
arr = {10,13,22,8,16,14}; m=4
Output
Maximum difference here is : 20
Explanation − Here the highest 4 numbers are 22,16,14,13 and the sum is 65. Lowest 4 numbers are 8,10,13,14 and the sum is 45 . So the highest or maximum difference is 65-45 which is 20.
Approach used in the below program as follows
Take input array arr[] and a number m for making sets
In the find_diff() function we are passing the input array and it’s length and returning the maximum difference of the sum of sets of m elements.
Here we will first sort the elements of array arr[].
Then we will find the sum of first m and last m elements as these will be least m and highest m numbers of arr[] .
At last we return the difference.
Note − sort(arr[],int) is assumed to return the sorted array.
Example
#include<stdio.h> //create function to calculate maximum difference between sum of highest and lowest m elements of array int find_diff(int arr[],int length,int m){ //for sorting the array sort(arr,length); int maxsum=0, minsum=0; //calculate now max difference between two subsets of m elements for(int i=0;i<m;i++){ minsum+=arr[i]; maxsum+=arr[length-i-1]; } return(maxsum-minsum); } // Driver program int main(){ int arr[]={1,1,2,3,5,7,1}; int m=3; cout << find_diff(arr,7,m); return 0; }
Output
If we run the above code we will get the following output −
12
- Related Articles
- Maximum possible difference of two subsets of an array in C++
- Maximize the difference between two subsets of a set with negatives in C
- Maximum difference of sum of elements in two rows in a matrix in C
- Maximum difference between two elements such that larger element appears after the smaller number in C
- Maximum and Minimum Product Subsets in C++
- Find set of m-elements with difference of any two elements is divisible by k in C++
- Maximum sum of difference of adjacent elements in C++
- C++ program to find minimum difference between the sums of two subsets from first n natural numbers
- Find maximum difference between nearest left and right smaller elements in C++
- Maximum difference between the group of k-elements and rest of the array in C
- Maximum element between two nodes of BST in C++
- Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 in C++
- Program to find the maximum difference between the index of any two different numbers in C++
- Count number of elements between two given elements in array in C++
