Tug of War in JavaIn the tug of war problem, we have to divide the given set of n integers into two sets of equal or almost equal sizes. The given set must be divided in such a way that the difference of the sum of integers present in the first subset and the second subset must be minimum. If the value of n is even, then the size of each set must be equal to n / 2. If the value of n is odd, then the size of one set must be n / 2, and the size of the other set is (n + 1) / 2. Let's understand it with the help of a few examples. Note: Minimum mentioned above means the value of the difference of the two subsets. In other words, the sign of the value has to be ignored. Only magnitude is considered.Example 1: S = {-3, 5, 4, 3, 1, 100, 54, 89, 20, 23}, where S is set that contains 10 elements. It is obvious that 10 is an even number. Thus, the two subsets that we will create from the given set must be of the size 5 (10 / 2 = 5) each. Those two subsets are: S1 = {4, 1, 100, 20, 23} and S2 = {-3, 5, 3, 54, 89} The sum of the subset S1 is 4 + 1 + 100 + 20 + 23 = 148 The sum of the subset S2 is -3 + 5 + 3 + 54 + 89 = 148 Thus, we see that sum of subset S1 is equal to the sum of subset S2. Thus, the difference of the two subsets is 148 - 148 = 0, which is minimum. Example 2: S = {-34, 23, 98, 45, 12, 4, 4, -99, -1, 189, 0}, where S is set that contains 11 elements. It is obvious that 11 is an odd number. Thus, the two subsets that we will create from the given set must be of the size 5 (10 / 2 = 5) and 6 (10 / 5 + 1 = 6), respectively. Those two subsets are: S1 = {45, -34, 12, 98, -1} and S2 = {23, 4, 4, -99, 189, 0} The sum of the subset S1 is 45 -34 + 12 + 98 -1 = 120 The sum of the subset S2 is 23 + 4 + 4 -99 + 189 + 0 = 121 Thus, we see that sum of subset S1 is equal to the sum of subset S2. Thus, the difference of the two subsets is 121 - 120 = 1. If we make any other pair of subsets of the same size, we cannot get the difference less than 1. Thus, 1 is minimum difference possible for the given set. ApproachThe following solution looks for each and every possible subset of the half size. If the first subset of the half of size is created, then the rest of the elements create another subset. We initialize the current set as an empty set and then fill elements in it. For every element, we have two possibilities: one is to include the element in the current set, and the other is to exclude the element from the current set. By considering these two possibilities, we fill the subsets. ImplementationLet's see the implementation of the tug of war problem using the approach mentioned above. FileName: TugOfWarEx.java Output: For the array: -34 23 98 45 12 4 4 -99 -1 189 0 The elements of the first subset are: -34 98 45 12 -1 The elements of the second subset are: 23 4 4 -99 189 0 For the array: -3 5 4 3 1 100 54 89 20 23 The elements of the first subset are: 4 1 100 20 23 The elements of the second subset are: -3 5 3 54 89 Time Complexity: The time complexity of the above program is 2n, where n is the number of elements present in the array. Next TopicClone HashMap in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India