forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum_Sum_Partition.cpp
75 lines (61 loc) · 1.79 KB
/
Minimum_Sum_Partition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Minimum Sum Partition
#include <iostream>
#include <string>
using namespace std;
/* Partition the Set into two subsets Set1, Set2 such that the
difference between the sum of elements in Set1 and the sum
of elements in Set2 is minimized */
int minPartition(int Set[], int n, int Set1, int Set2)
{
/* Case 0. If list becomes empty, return the absolute
difference between two sets */
if (n < 0)
return abs(Set1 - Set2);
/* Case 1. Include current item in the subset Se1 and recur
for remaining items (n - 1) */
int inc = minPartition(Set, n - 1, Set1 + Set[n], Set2);
/* Case 2. Exclude current item from subset Se1 and recur for
remaining items (n - 1) */
int exc = minPartition(Set, n - 1, Set1, Set2 + Set[n]);
//Returning included and excluded values
return min(inc, exc);
}
//Main Code
int main()
{
//int Set[] = { 10, 20, 15, 5, 25};
int size;
std::cout << "Enter the number of elements you want in Set: ";
std::cin >> size;
int Set[size]; //Creating Set of size 'size'
std::cout << "Enter elements: " << std::endl;
for (int i = 0; i < size; i++) //Enter the elements in the Set
{
std::cin >> Set[i];
}
// Number of elements
int n = sizeof(Set) / sizeof(Set[0]);
cout << "\nThe entered elements in the Set are: \n" << std::endl;
for (int i = 0; i < size; ++i) {
cout << Set[i] << " ";
}
//Printing the minimum difference
cout << "\nThe minimum difference is " << minPartition(Set, n - 1, 0, 0);
return 0;
}
/*
SAMPLE INPUT:
5
1
2
3
4
5
SAMPLE OUTPUT:
Enter the number of elements you want in Set: Enter elements:
The entered elements in the Set are:
1 2 3 4 5
The minimum difference is 1
Time Complexity = O(n*sum)
Depends on the value of 'n' and 'sum'
*/