Given a set of n integers, divide the set in two subsets of n/2 sizes each such that the difference of the sum of two subsets is as minimum as possible. If n is even, then sizes of two subsets must be strictly n/2 and if n is odd, then size of one subset must be (n-1)/2 and size of other subset must be (n+1)/2.

For example, 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.

The following solution tries every possible subset of half size. If one subset of half size is formed, the remaining elements form the other subset. We initialize current set as empty and one by one build it. There are two possibilities for every element, either it is part of current set, or it is part of the remaining elements (other subset). We consider both possibilities for every element. When the size of current set becomes n/2, we check whether this solutions is better than the best solution available so far. If it is, then we update the best solution.

[ad type=”banner”]

Following is C++ implementation for Tug of War problem. It prints the required arrays.

[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Climits.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20function%20that%20tries%20every%20possible%20solution%20by%20calling%20itself%20recursively%0Avoid%20TOWUtil(int*%20arr%2C%20int%20n%2C%20bool*%20curr_elements%2C%20int%20no_of_selected_elements%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20bool*%20soln%2C%20int*%20min_diff%2C%20int%20sum%2C%20int%20curr_sum%2C%20int%20curr_position)%0A%7B%0A%20%20%20%20%2F%2F%20checks%20whether%20the%20it%20is%20going%20out%20of%20bound%0A%20%20%20%20if%20(curr_position%20%3D%3D%20n)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F%2F%20checks%20that%20the%20numbers%20of%20elements%20left%20are%20not%20less%20than%20the%0A%20%20%20%20%2F%2F%20number%20of%20elements%20required%20to%20form%20the%20solution%0A%20%20%20%20if%20((n%2F2%20-%20no_of_selected_elements)%20%3E%20(n%20-%20curr_position))%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F%2F%20consider%20the%20cases%20when%20current%20element%20is%20not%20included%20in%20the%20solution%0A%20%20%20%20TOWUtil(arr%2C%20n%2C%20curr_elements%2C%20no_of_selected_elements%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20soln%2C%20min_diff%2C%20sum%2C%20curr_sum%2C%20curr_position%2B1)%3B%0A%20%0A%20%20%20%20%2F%2F%20add%20the%20current%20element%20to%20the%20solution%0A%20%20%20%20no_of_selected_elements%2B%2B%3B%0A%20%20%20%20curr_sum%20%3D%20curr_sum%20%2B%20arr%5Bcurr_position%5D%3B%0A%20%20%20%20curr_elements%5Bcurr_position%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20checks%20if%20a%20solution%20is%20formed%0A%20%20%20%20if%20(no_of_selected_elements%20%3D%3D%20n%2F2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20checks%20if%20the%20solution%20formed%20is%20better%20than%20the%20best%20solution%20so%20far%0A%20%20%20%20%20%20%20%20if%20(abs(sum%2F2%20-%20curr_sum)%20%3C%20*min_diff)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20*min_diff%20%3D%20abs(sum%2F2%20-%20curr_sum)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20soln%5Bi%5D%20%3D%20curr_elements%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20consider%20the%20cases%20where%20current%20element%20is%20included%20in%20the%20solution%0A%20%20%20%20%20%20%20%20TOWUtil(arr%2C%20n%2C%20curr_elements%2C%20no_of_selected_elements%2C%20soln%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_diff%2C%20sum%2C%20curr_sum%2C%20curr_position%2B1)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20removes%20current%20element%20before%20returning%20to%20the%20caller%20of%20this%20function%0A%20%20%20%20curr_elements%5Bcurr_position%5D%20%3D%20false%3B%0A%7D%0A%20%0A%2F%2F%20main%20function%20that%20generate%20an%20arr%0Avoid%20tugOfWar(int%20*arr%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20the%20boolen%20array%20that%20contains%20the%20inclusion%20and%20exclusion%20of%20an%20element%0A%20%20%20%20%2F%2F%20in%20current%20set.%20The%20number%20excluded%20automatically%20form%20the%20other%20set%0A%20%20%20%20bool*%20curr_elements%20%3D%20new%20bool%5Bn%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20The%20inclusion%2Fexclusion%20array%20for%20final%20solution%0A%20%20%20%20bool*%20soln%20%3D%20new%20bool%5Bn%5D%3B%0A%20%0A%20%20%20%20int%20min_diff%20%3D%20INT_MAX%3B%0A%20%0A%20%20%20%20int%20sum%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20sum%20%2B%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20curr_elements%5Bi%5D%20%3D%20%20soln%5Bi%5D%20%3D%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Find%20the%20solution%20using%20recursive%20function%20TOWUtil()%0A%20%20%20%20TOWUtil(arr%2C%20n%2C%20curr_elements%2C%200%2C%20soln%2C%20%26min_diff%2C%20sum%2C%200%2C%200)%3B%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20solution%0A%20%20%20%20cout%20%3C%3C%20%22The%20first%20subset%20is%3A%20%22%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(soln%5Bi%5D%20%3D%3D%20true)%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20%22%5CnThe%20second%20subset%20is%3A%20%22%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(soln%5Bi%5D%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B23%2C%2045%2C%20-34%2C%2012%2C%200%2C%2098%2C%20-99%2C%204%2C%20189%2C%20-1%2C%204%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20tugOfWar(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++ Program” highlight=”” provider=”manual”/]

Output:

The first subset is: 45 -34 12 98 -1
The second subset is: 23 0 -99 4 189 4
[ad type=”banner”]