{"id":26393,"date":"2017-10-26T21:51:00","date_gmt":"2017-10-26T16:21:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26393"},"modified":"2017-10-26T21:51:00","modified_gmt":"2017-10-26T16:21:00","slug":"c-programming-tug-war","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-tug-war\/","title":{"rendered":"C++ Programming &#8211; Tug of War"},"content":{"rendered":"<p>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. <span id=\"more-115594\"><\/span>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.<\/p>\n<p>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).<br \/>\nLet 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.<\/p>\n<p>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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation for Tug of War problem. It prints the required arrays.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include &lt;iostream&gt;<br\/>#include &lt;stdlib.h&gt;<br\/>#include &lt;limits.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ function that tries every possible solution by calling itself recursively<br\/>void TOWUtil(int* arr, int n, bool* curr_elements, int no_of_selected_elements,<br\/>             bool* soln, int* min_diff, int sum, int curr_sum, int curr_position)<br\/>{<br\/>    \/\/ checks whether the it is going out of bound<br\/>    if (curr_position == n)<br\/>        return;<br\/> <br\/>    \/\/ checks that the numbers of elements left are not less than the<br\/>    \/\/ number of elements required to form the solution<br\/>    if ((n\/2 - no_of_selected_elements) &gt; (n - curr_position))<br\/>        return;<br\/> <br\/>    \/\/ consider the cases when current element is not included in the solution<br\/>    TOWUtil(arr, n, curr_elements, no_of_selected_elements,<br\/>              soln, min_diff, sum, curr_sum, curr_position+1);<br\/> <br\/>    \/\/ add the current element to the solution<br\/>    no_of_selected_elements++;<br\/>    curr_sum = curr_sum + arr[curr_position];<br\/>    curr_elements[curr_position] = true;<br\/> <br\/>    \/\/ checks if a solution is formed<br\/>    if (no_of_selected_elements == n\/2)<br\/>    {<br\/>        \/\/ checks if the solution formed is better than the best solution so far<br\/>        if (abs(sum\/2 - curr_sum) &lt; *min_diff)<br\/>        {<br\/>            *min_diff = abs(sum\/2 - curr_sum);<br\/>            for (int i = 0; i&lt;n; i++)<br\/>                soln[i] = curr_elements[i];<br\/>        }<br\/>    }<br\/>    else<br\/>    {<br\/>        \/\/ consider the cases where current element is included in the solution<br\/>        TOWUtil(arr, n, curr_elements, no_of_selected_elements, soln,<br\/>                  min_diff, sum, curr_sum, curr_position+1);<br\/>    }<br\/> <br\/>    \/\/ removes current element before returning to the caller of this function<br\/>    curr_elements[curr_position] = false;<br\/>}<br\/> <br\/>\/\/ main function that generate an arr<br\/>void tugOfWar(int *arr, int n)<br\/>{<br\/>    \/\/ the boolen array that contains the inclusion and exclusion of an element<br\/>    \/\/ in current set. The number excluded automatically form the other set<br\/>    bool* curr_elements = new bool[n];<br\/> <br\/>    \/\/ The inclusion\/exclusion array for final solution<br\/>    bool* soln = new bool[n];<br\/> <br\/>    int min_diff = INT_MAX;<br\/> <br\/>    int sum = 0;<br\/>    for (int i=0; i&lt;n; i++)<br\/>    {<br\/>        sum += arr[i];<br\/>        curr_elements[i] =  soln[i] = false;<br\/>    }<br\/> <br\/>    \/\/ Find the solution using recursive function TOWUtil()<br\/>    TOWUtil(arr, n, curr_elements, 0, soln, &amp;min_diff, sum, 0, 0);<br\/> <br\/>    \/\/ Print the solution<br\/>    cout &lt;&lt; &quot;The first subset is: &quot;;<br\/>    for (int i=0; i&lt;n; i++)<br\/>    {<br\/>        if (soln[i] == true)<br\/>            cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;<br\/>    }<br\/>    cout &lt;&lt; &quot;\\nThe second subset is: &quot;;<br\/>    for (int i=0; i&lt;n; i++)<br\/>    {<br\/>        if (soln[i] == false)<br\/>            cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;<br\/>    }<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    int arr[] = {23, 45, -34, 12, 0, 98, -99, 4, 189, -1, 4};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    tugOfWar(arr, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>The first subset is: 45 -34 12 98 -1\r\nThe second subset is: 23 0 -99 4 189 4<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Tug of War &#8211; Backtracking &#8211; Given a set of n integers, divide the set in two subsets of n\/2 sizes each such that the difference of the sum <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73907,83515,1],"tags":[78613,78615,78635,78625,78622,78629,78608,78603,78602,78611,78623,78621,78606,78600],"class_list":["post-26393","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-backtracking-algorithm","category-c-programming-3","category-coding","tag-2-player-tug-of-war-games","tag-a-game-of-tug-of-war-is-an-example-of","tag-multiplication-tug-of-war","tag-nrich-tug-of-war","tag-physics-behind-tug-of-war","tag-physics-of-tug-of-war","tag-three-way-tug-of-war","tag-tug-of-war-strategy","tag-tug-of-war-technique","tag-tug-of-war-vector","tag-what-does-it-take-to-win-a-tug-of-war","tag-what-is-tug-in-tug-of-war","tag-what-is-tug-of-war","tag-what-is-tug-of-war-game"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26393"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26393\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}