Professional Documents
Culture Documents
Aim:- A problem to divide a set of number to two different set where both the subset contains
same number of element and have a minimum difference of sum between them using
backtracking.
Theory:-
In this problem a set of integers are given, we have to break them into two parts, such that the
difference of the sum of two subsets is minimum as possible. So our target is to divide two
groups of nearly equal strength to participate in the Tug of war game.
If the size of subset n is even, it must be divided into n/2, but for the odd value of n, then the size
of one subset must be (n-1)/2, and size of another subset must be (n+1)/2.
There is a set contains N number of elements. We have to divide this set into two different sets
where each of the subsets contains the same number of elements and has a minimum difference
between them.
1. If N is even then each of the subsets will contain N/2 number of elements.
2. If N is odd then one of the subsets will contain (N+1)/2 and others will contain (N-
1)/2 number of elements.
Explanation:-
let given set be {3, 4, 5, -3, 100, 1, 89, 54, 23, 20}, the size of set is 10. Output for this set
should be {4, 100, 1, 23, 20} and {3, 5, -3, 89, 54}. Both output subsets are of size 5 and sum of
elements in both subsets is same (148 and 148).Let us consider another example where n is odd.
Let given set be {23, 45, -34, 12, 0, 98, -99, 4, 189, -1, 4}. The output subsets should be {45, -
34, 12, 98, -1} and {23, 0, -99, 4, 189, 4}. The sums of elements in two subsets are 120 and 121
respectively.
Input
The input begins with a single positive integer on a line by itself indicating the number of the
cases following, each of them as described below. This line is followed by a blank line, and
there is also a blank line between two consecutive inputs.The first line of input contains n the
number of people at the picnic. n lines follow. The first linegives the weight of person 1; the
second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There
are at most 100 people at the picnic.
Output
For each test case, the output must follow the description below. The outputs of two consecutive
cases will be separated by a blank line.Your output will be a single line containing 2 numbers:
the total weight of the people on one team,and the total weight of the people on the other team.
If these numbers differ, give the lesser first.
Example
T=3
N=3
123
N=4
1234
N=10
1 4 8 6 -7 -10 87 54 16 100
Output: set1: {{ 4, -7, -10, 87, 54 } set2: {1, 8, 6, 16, 100 }
Sample Input
1
3
100
90
200
Sample Output
190 200
Conclusion:
In this problem to divide a set of number to two different set where both the subset contains
same number of element and have a minimum difference of sum.
/*Student Name: Hitesh Raghunath Dhake Class: T.E
Roll_no: 16 Batch: T1
min_diff = [999999999999]
Sum = 0
for i in range(n):
Sum += arr[i]
curr_elements[i] = soln[i] = False
# Find the solution using recursive
# function TOWUtil()
TOWUtil(arr, n, curr_elements, 0,
soln, min_diff, Sum, 0, 0)
# Driver Code
if __name__ == '__main__':