{"id":2256,"date":"2020-06-15T01:37:06","date_gmt":"2020-06-15T01:37:06","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=2256"},"modified":"2022-03-23T07:53:06","modified_gmt":"2022-03-23T07:53:06","slug":"tug-of-war","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/tug-of-war\/","title":{"rendered":"Tug of War"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png\" alt=\"\" \/><\/p>\n<h3>Concepts Used<\/h3>\n<blockquote>\n<p>Back Tracking<\/p>\n<\/blockquote>\n<h3>Difficulty Level<\/h3>\n<blockquote>\n<p>Hard<\/p>\n<\/blockquote>\n<h3>Problem Statement :<\/h3>\n<blockquote>\n<p>Given a set of <code>n<\/code> integers, divide the set in two subsets of <code>n\/2<\/code> sizes each such that the difference of the sum of two subsets is as minimum as possible. If <code>n<\/code> is even, then sizes of two subsets must be strictly <code>n\/2<\/code> and if <code>n<\/code> is odd, then size of one subset must be <code>(n-1)\/2<\/code> and size of other subset must be <code>(n+1)\/2<\/code>. Print YES if it is possible to get the absolute minimum difference exactly <code>0<\/code>, else print NO.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/back-tracking\/TOW\" title=\"Go to mycode.prepbytes.com\" target=\"_blank\" rel=\"noopener noreferrer\"><u><strong><\/strong><\/u><\/a><\/p>\n<h3>Solution Approach :<\/h3>\n<h4>Introduction :<\/h4>\n<blockquote>\n<p>Idea is to divide the whole set into two halves. We will try every possible subset to fill the first half of the set, once the half set is filled we can say the remaining elements belongs to the other half. We have to keep the absolute difference minimum possible (in our case its <code>0<\/code>).<br \/>\nFor example,  given set, <strong>{20, 0, 19, 2 ,-20, -3, 4 ,-14}<\/strong>, the size of set is <code>8<\/code>. Output for this set should be <strong>&quot;YES&quot;<\/strong>. <strong>{19,2,-3,-14}<\/strong> and <strong>{20,0,-20,4}<\/strong> are the two sets formed. Both output subsets are of size <code>4<\/code> and sum of elements in both subsets is <code>4<\/code> and <code>4<\/code> respectively. <code>(4-4=0)<\/code>.<\/p>\n<\/blockquote>\n<h4>Description:<\/h4>\n<blockquote>\n<p>We will use backtracking to solve the above problem.<br \/>\n<strong>Backtracking<\/strong> is the approach to solve the problem by testing all possible answers (by breaking the problems into subproblems). If any subproblem does not fit the given constraint then we discard the complete subproblem (backtrack), moves a step back then try other remaining possible combinations. Backtracking algorithm is generally exponential in time.<\/p>\n<p>As mentioned above we will divide the whole set into two and try every possible subset, once we make a subset with length <code>n\/2<\/code> or <code>(n-1)\/2<\/code> depending upon the size is even or odd, we stop and store the difference of both the subsets. Every element must either belong to the <strong>subset 1<\/strong> or <strong>subset 2<\/strong>. We continue doing this for every subset. Once we are done we&#8217;ll have the minimum possible absolute difference. Aditionally, we also keep track of the elements which are chosen for <strong>subset 1<\/strong> (remaining elements automatically falls into <strong>subset 2<\/strong>) using a curr _element[ ] array. If the value in array in <code>ith<\/code> index is <code>1<\/code> we will say the element at i<sup>th<\/sup> index belongs to <strong>subset 1<\/strong> else it belongs to <strong>subset 2<\/strong>.<\/p>\n<\/blockquote>\n<h3>Algorithm :<\/h3>\n<h4>tow():<\/h4>\n<ol>\n<li>if <code>index==size<\/code>, return<\/li>\n<li>recurvisely call <code>tow()<\/code> for next index.<\/li>\n<li>If <code>index == size\/2<\/code>, store the absolute difference if it is less than the previous difference.<\/li>\n<li>include the current element in sum , <code>sum = sum + a[index]<\/code><\/li>\n<li>assign <strong>subset 1<\/strong> to the current index, <code>(currElement[index] = true)<\/code>.<\/li>\n<li>recurvively call <code>tow<\/code> for next index.<\/li>\n<li>unassign the current index from <strong>subset 1<\/strong> <code>(currElement[index] = false)<\/code>.<\/li>\n<\/ol>\n<h3>Complexity Analysis :<\/h3>\n<blockquote>\n<p>We are making two recursive calls each time and for every new call  again 2 recursive calls . So the <strong>time complexity<\/strong> would grow exponentially! Can you guess the time complexity?<\/p>\n<\/blockquote>\n<h3>Solutions:<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_2257 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_2257 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_2257 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_2257 .wpsm_nav-tabs > li.active > a, #tab_container_2257 .wpsm_nav-tabs > li.active > a:hover, #tab_container_2257 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_2257 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_2257 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_2257 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_2257 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_2257 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_2257 .wpsm_nav-tabs > li > a:hover , #tab_container_2257 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_2257 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_2257 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_2257 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_2257 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_2257 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_2257 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_2257 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_2257 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_2257 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_2257 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_2257 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_2257\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_2257\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_2257_1\" aria-controls=\"tabs_desc_2257_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_2257_2\" aria-controls=\"tabs_desc_2257_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_2257_3\" aria-controls=\"tabs_desc_2257_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_2257\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_2257_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n#define INT_MAX 999999\r\n\r\nvoid TOWUtil(int* arr, int n, int* curr_elements, int no_of_selected_elements, \r\n            int* soln, int* min_diff, int sum, int curr_sum, int curr_position) \r\n{ \r\n\r\n    if (curr_position == n) \r\n        return; \r\n\r\n    if ((n\/2 - no_of_selected_elements) &gt; (n - curr_position)) \r\n        return; \r\n\r\n    TOWUtil(arr, n, curr_elements, no_of_selected_elements, \r\n            soln, min_diff, sum, curr_sum, curr_position+1); \r\n\r\n\r\n    no_of_selected_elements++; \r\n    curr_sum = curr_sum + arr[curr_position]; \r\n    curr_elements[curr_position] = 1; \r\n\r\n    if (no_of_selected_elements == n\/2) \r\n    { \r\n        if (abs(sum\/2 - curr_sum) &lt; *min_diff) \r\n        { \r\n            *min_diff = abs(sum\/2 - curr_sum); \r\n            for (int i = 0; i&lt;n; i++) \r\n                soln[i] = curr_elements[i]; \r\n        } \r\n    } \r\n    else\r\n    { \r\n\r\n        TOWUtil(arr, n, curr_elements, no_of_selected_elements, soln, \r\n                min_diff, sum, curr_sum, curr_position+1); \r\n    } \r\n\r\n    \/\/ removes current element before returning to the caller of this function \r\n    curr_elements[curr_position] = 0; \r\n} \r\n\r\n\/\/ main function that generate an arr \r\nvoid tugOfWar(int *arr, int n) \r\n{ \r\n\r\n    int* curr_elements = (int *)malloc(sizeof(int)*n);\r\n\r\n    \/\/ The inclusion\/exclusion array for final solution \r\n    int* soln = (int *)malloc(sizeof(int)*n);\r\n\r\n    int min_diff = INT_MAX; \r\n\r\n    int sum = 0; \r\n    for (int i=0; i&lt;n; i++) \r\n    { \r\n        sum += arr[i]; \r\n        curr_elements[i] = soln[i] = 0; \r\n    } \r\n\r\n    TOWUtil(arr, n, curr_elements, 0, soln, &amp;min_diff, sum, 0, 0); \r\n\r\n    int sum1=0;\r\n    for (int i=0; i&lt;n; i++) \r\n    { \r\n        if (soln[i] == 1) \r\n            sum1 += arr[i];\r\n    } \r\n   int sum2 = 0;\r\n    for (int i=0; i&lt;n; i++) \r\n    { \r\n        if (soln[i] == 0) \r\n           sum2 +=arr[i]; \r\n    } \r\n\r\n    if(sum1-sum2==0)\r\n      printf(&quot;%s&#92;n&quot;,&quot;YES&quot;);\r\n    else\r\n      printf(&quot;%s&#92;n&quot;,&quot;NO&quot;);\r\n\r\n} \r\n\r\nint main() \r\n{ \r\n  int t;\r\n  scanf(&quot;%d&quot;,&amp;t);\r\n  while(t--)\r\n  {\r\n    int n;\r\n    scanf(&quot;%d&quot;,&amp;n);\r\n    int arr[n]; \r\n    for(int i=0;i&lt;n;i++)\r\n     scanf(&quot;%d&quot;,&amp;arr[i]);\r\n\r\n      tugOfWar(arr, n); \r\n  }\r\n    return 0; \r\n} \r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_2257_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\n    #include &lt;stdlib.h&gt;\r\n    #include &lt;limits.h&gt;\r\n \r\n    using namespace std;\r\n \r\n \r\n    void TugofWarRecur(int* array, int n, bool* current_elements, int selected_elements_count,bool* solution, int* min_diff, int sum, int current_sum, int current_position)\r\n    {\r\n \r\n        if (current_position == n)\r\n        {\r\n            return;\r\n        }\r\n        if ((n\/2 - selected_elements_count) &gt; (n - current_position))\r\n        {\r\n            return;\r\n        }\r\n \r\n        TugofWarRecur(array, n, current_elements, selected_elements_count,solution, min_diff, sum, current_sum, current_position+1);\r\n \r\n        selected_elements_count++;\r\n        current_sum = current_sum + array[current_position];\r\n        current_elements[current_position] = true;\r\n        if (selected_elements_count == n\/2)\r\n        {\r\n            if (abs(sum\/2 - current_sum) &lt; *min_diff)\r\n            {\r\n                *min_diff = abs(sum\/2 - current_sum);\r\n                for (int i = 0; i&lt;n; i++)\r\n                {\r\n                    solution[i] = current_elements[i];\r\n                }\r\n            }\r\n        }\r\n        else\r\n        {\r\n            TugofWarRecur(array, n, current_elements, selected_elements_count, solution, min_diff, sum, current_sum, current_position+1);\r\n        }\r\n        current_elements[current_position] = false;\r\n    }\r\n \r\n    void TugOfWar(int *array, int n)\r\n    {\r\n        bool* current_elements = new bool[n];\r\n        bool* solution = new bool[n];\r\n        int min_diff = INT_MAX;\r\n        int sum = 0;\r\n        for (int i=0; i&lt;n; i++)\r\n        {\r\n            sum =sum + array[i];\r\n            current_elements[i] =  solution[i] = false;\r\n        }\r\n        TugofWarRecur(array, n, current_elements, 0, solution, &amp;min_diff, sum, 0, 0);\r\n \r\n        int sumAdd=0;\r\n        for (int i=0; i&lt;n; i++)\r\n        {\r\n            if(solution[i] == true)\r\n            {\r\n                sumAdd += array[i];\r\n            }\r\n        }\r\n        int sumAdd1=0;\r\n        for (int i=0; i&lt;n; i++)\r\n        {\r\n            if(solution[i] == false)\r\n            {\r\n                sumAdd1 += array[i];\r\n            }\r\n        }\r\n        if(abs(sumAdd-sumAdd1) ==0)\r\n            cout&lt;&lt;&quot;YES&quot;&lt;&lt;&quot;&#92;n&quot;;\r\n        else\r\n            cout&lt;&lt;&quot;NO&quot;&lt;&lt;&quot;&#92;n&quot;;\r\n    }\r\n \r\n    \/\/Main function\r\n    int main()\r\n    {\r\n        int t;\r\n        cin&gt;&gt;t;\r\n        while(t--)\r\n        {\r\n            int n;\r\n            cin&gt;&gt;n;\r\n            int arr[n];\r\n            for(int i=0;i&lt;n;i++)\r\n                cin&gt;&gt;arr[i];\r\n            TugOfWar(arr,n);\r\n        }\r\n \r\n        return 0;\r\n    }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_2257_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class Main {\r\n\r\n    static boolean []cur_ele;\r\n    static  boolean []sol;\r\n    static int min_diff=Integer.MAX_VALUE;\r\n    public static void main(String[] args) {\r\n    \/\/ write your code here\r\n        Scanner sc = new Scanner(System.in);\r\n        int t=sc.nextInt();\r\n        while(t--&gt;0)\r\n        {\r\n\r\n            int n =sc.nextInt();\r\n            int []arr = new int[n];\r\n            for(int i=0;i&lt;n;i++)\r\n                arr[i]=sc.nextInt();\r\n            TugOfWar(arr,n);\r\n\r\n            int sum1=0;\r\n            for(int i=0;i&lt;n;i++)\r\n            {\r\n                if(sol[i]==true)\r\n                    sum1+=arr[i];\r\n            }\r\n\r\n            int sum2=0;\r\n            for(int i=0;i&lt;n;i++)\r\n            {\r\n                if(sol[i]==false)\r\n                    sum2+=arr[i];\r\n            }\r\n            if(Math.abs(sum1-sum2)==0)\r\n                System.out.println(&quot;YES&quot;);\r\n            else\r\n                System.out.println(&quot;NO&quot;);\r\n        }\r\n    }\r\n\r\n    static void TugOfWar(int arr[] , int n)\r\n    {\r\n        cur_ele = new boolean[n];\r\n        sol = new boolean[n];\r\n        min_diff=Integer.MAX_VALUE;\r\n        int sum=0;\r\n        for(int i=0;i&lt;n;i++)\r\n        {\r\n            sum+=arr[i];\r\n            cur_ele[i]=sol[i]=false;\r\n        }\r\n        TugOfWarRecur(arr,n,0,sum,0,0);\r\n    }\r\n\r\n    static void TugOfWarRecur(int arr[], int n, int selected, int sum, int curr_sum, int curr_pos )\r\n    {\r\n        if(curr_pos==n)\r\n            return;\r\n\r\n        selected++;\r\n        curr_sum=curr_sum+arr[curr_pos];\r\n        cur_ele[curr_pos]=true;\r\n        if(selected==n\/2)\r\n        {\r\n            if(Math.abs(sum\/2-curr_sum)&lt;min_diff)\r\n            {\r\n                min_diff = Math.abs(sum\/2-curr_sum);\r\n                for(int i=0;i&lt;n;i++)\r\n                    sol[i]=cur_ele[i];\r\n            }\r\n        }\r\n\r\n        TugOfWarRecur(arr,n,selected,sum,curr_sum,curr_pos+1);\r\n\r\n        selected--;\r\n        curr_sum=curr_sum-arr[curr_pos];\r\n        cur_ele[curr_pos]=false;\r\n        TugOfWarRecur(arr,n,selected,sum,curr_sum,curr_pos+1);\r\n\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_2257 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_2257 a\"),jQuery(\"#tab-content_2257\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p>[forminator_quiz id=&quot;2258&quot;]<\/p>\n<p>This article tried to discuss the concept of Backtracking. Hope this blog helps you understand and solve the problem. To practice more problems on Backtracking you can check out <a href=\"#\"><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Concepts Used Back Tracking Difficulty Level Hard Problem Statement : 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 [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[116],"tags":[],"class_list":["post-2256","post","type-post","status-publish","format-standard","hentry","category-backtracking-interview-questions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Backtracking Interview Questions | Tug of War | Prepbytes<\/title>\n<meta name=\"description\" content=\"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.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Backtracking Interview Questions | Tug of War | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"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.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-15T01:37:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-23T07:53:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Tug of War\",\"datePublished\":\"2020-06-15T01:37:06+00:00\",\"dateModified\":\"2022-03-23T07:53:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\"},\"wordCount\":484,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png\",\"articleSection\":[\"Backtracking Interview Questions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\",\"name\":\"Backtracking Interview Questions | Tug of War | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png\",\"datePublished\":\"2020-06-15T01:37:06+00:00\",\"dateModified\":\"2022-03-23T07:53:06+00:00\",\"description\":\"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.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/tug-of-war\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tug-of-war\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Backtracking Interview Questions\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/backtracking-interview-questions\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Tug of War\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Backtracking Interview Questions | Tug of War | Prepbytes","description":"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.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/tug-of-war\/","og_locale":"en_US","og_type":"article","og_title":"Backtracking Interview Questions | Tug of War | Prepbytes","og_description":"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.","og_url":"https:\/\/prepbytes.com\/blog\/tug-of-war\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-06-15T01:37:06+00:00","article_modified_time":"2022-03-23T07:53:06+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Tug of War","datePublished":"2020-06-15T01:37:06+00:00","dateModified":"2022-03-23T07:53:06+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/"},"wordCount":484,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png","articleSection":["Backtracking Interview Questions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/tug-of-war\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/","url":"https:\/\/prepbytes.com\/blog\/tug-of-war\/","name":"Backtracking Interview Questions | Tug of War | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png","datePublished":"2020-06-15T01:37:06+00:00","dateModified":"2022-03-23T07:53:06+00:00","description":"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.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/tug-of-war\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1647244065848-Article_516.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/tug-of-war\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Backtracking Interview Questions","item":"https:\/\/prepbytes.com\/blog\/category\/backtracking-interview-questions\/"},{"@type":"ListItem","position":3,"name":"Tug of War"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/2256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=2256"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/2256\/revisions"}],"predecessor-version":[{"id":8171,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/2256\/revisions\/8171"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=2256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=2256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=2256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}