- 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
Maximum possible difference of two subsets of an array in C++
In this tutorial, we will be discussing a program to find maximum possible difference of two subsets of an array
For this we will be provided with an array containing one or two instances of few random integers. Our task is to create two subsets of that array such that the difference of their sum is maximum and no subset contains repetitive numbers.
Example
#include <bits/stdc++.h> using namespace std; //finding maximum subset difference int maxDiff(int arr[], int n) { int SubsetSum_1 = 0, SubsetSum_2 = 0; for (int i = 0; i <= n - 1; i++) { bool isSingleOccurance = true; for (int j = i + 1; j <= n - 1; j++) { if (arr[i] == arr[j]) { isSingleOccurance = false; arr[i] = arr[j] = 0; break; } } if (isSingleOccurance) { if (arr[i] > 0) SubsetSum_1 += arr[i]; else SubsetSum_2 += arr[i]; } } return abs(SubsetSum_1 - SubsetSum_2); } int main() { int arr[] = { 4, 2, -3, 3, -2, -2, 8 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Maximum Difference = " << maxDiff(arr, n); return 0; }
Output
Maximum Difference = 20
- Related Articles
- Maximum difference between two subsets of m elements in C
- Finding all possible subsets of an array in JavaScript
- Maximum possible XOR of every element in an array with another array in C++
- Sum of XOR of all possible subsets in C++
- Sum of the products of all possible Subsets in C++
- Maximum XOR of Two Numbers in an Array in C++
- Maximize the difference between two subsets of a set with negatives in C
- Find the sum of maximum difference possible from all subset of a given array in Python
- Maximum and Minimum Product Subsets in C++
- Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++
- Maximum difference between first and last indexes of an element in array in C
- What is the maximum possible value of an integer in C# ?
- Maximum Sum of Products of Two Array in C++ Program
- Find the maximum possible value of the minimum value of modified array in C++
- Maximum product subset of an array in C++

Advertisements