- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximize the difference between two subsets of a set with negatives in C
We are given with an array of positive and negative integers. The task is to find the maximum difference between positive and negative subsets of elements present in the array. As we have subsets of positive and negative numbers. Then the difference (sum of positives) - (sum of negatives) will always be maximum. This is because subtracting negatives will add them. Converting all negatives into positive and adding all the elements of the array will produce the desired result. Let us see examples for understanding −
Input − Arr[] = { -2, 0, -3, 8, 10, 12, -4 }
Output − Maximized difference between two subsets − 39
Explanation − positive integers subset {0, 8,10,12} sum is 30
Negative integers subset { -2, -3, -4 } sum is -9
Maximum difference will be 30 - (-9) = 39
Input − Arr[] = { -5, -15, -3, -2, 10, 20, 15 }
Output − Maximized difference between two subsets − 70
Explanation − positive integers subset { 10, 20, 15 } sum is 45
Negative integers subset { -5, -15, -3, -2 } sum is -25
Maximum difference will be 45 - (-25) = 70
Approach used in the below program is as follows
We take an integer array having positive and negative integers as Arr[]
The function subsetDifference( int arr[],int n) is to find the maximized difference between two subsets of negative and positive integers. It takes two arguments, one is the array itself and the other is its size n.
Take a variable sum=0 to store the sum of all elements of the array.
Starting from left, traverse each element of array using for loop ( i=0;i<n;i++ )
If current element is negative (<0) make it positive by multiplying with -1.( arr[i]=arr[i]*-1 )
Add each element to the sum.
Return the sum as the maximum subset difference possible.
Example
#include <stdio.h> int subsetDifference(int arr[], int n){ int sum = 0; for (int i = 0; i < n; i++){ if(arr[i]<0) arr[i]=arr[i]*-1; sum += arr[i]; } return sum; } // Driver Code int main(){ int arr[] = { -1, 3, 5, 17, -32, 12 }; int n = 6; printf("Maximized difference between subsets : %d", subsetDifference(arr, n)); return 0; }
Output
If we run the above code it will generate the following output −
Maximized difference between two subsets: 70
- Related Articles
- Maximum difference between two subsets of m elements in C
- Maximum possible difference of two subsets of an array in C++
- C++ program to find minimum difference between the sums of two subsets from first n natural numbers
- Find all distinct subsets of a given set in C++
- Count number of subsets of a set with GCD equal to a given number in C++
- Print all subsets of given size of a set in C++
- Minimum flips required to maximize a number with k set bits in C++.
- Maximize the sum of products of the degrees between any two vertices of the tree in C++
- List all the subsets of a set {m , n}
- Count number of ways to partition a set into k subsets in C++
- C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
- C++ Program to Implement the Binary Counting Method to Generate Subsets of a Set
- How to find all subsets of a set in JavaScript?
- Set a border between two lines with CSS
- Difference between Set and MultiSet in C++
