Maximize the difference between two subsets of a set with negatives in C

CServer Side ProgrammingProgramming

<p style="">We are given with an array of positive and negative integers. The task is to find the maximum difference between positive and negative subsets of elements present in the array. As we have subsets of positive and negative numbers. Then the difference (sum of positives) - (sum of negatives) will always be maximum. This is because subtracting negatives will add them. Converting all negatives into positive and adding all the elements of the array will produce the desired result. Let us see examples for understanding &minus;</p><p><strong>Input</strong> &minus; Arr[] = { -2, 0, -3, 8, 10, 12, -4 }</p><p><strong>Output</strong> &minus; Maximized difference between two subsets &minus; 39</p><p><strong>Explanation</strong> &minus; positive integers subset {0, 8,10,12} sum is 30</p><p style="">Negative integers subset { -2, -3, -4 } sum is -9</p><p>Maximum difference will be 30 - (-9) = 39</p><p><strong>Input</strong> &minus; Arr[] = { -5, -15, -3, -2, 10, 20, 15 }</p><p><strong>Output</strong> &minus; Maximized difference between two subsets &minus; 70</p><p><strong>Explanation</strong> &minus; positive integers subset { 10, 20, 15 } sum is 45</p><p>Negative integers subset { -5, -15, -3, -2 } sum is -25</p><p>Maximum difference will be 45 - (-25) = 70</p><h2>Approach used in the below program is as follows</h2><ul class="list"><li><p>We take an integer array having positive and negative integers as Arr[]</p></li><li><p>The function subsetDifference( int arr[],int n) is to find the maximized difference between two subsets of negative and positive integers. It takes two arguments, one is the array itself and the other is its size n.</p></li><li><p>Take a variable sum=0 to store the sum of all elements of the array.</p></li><li><p>Starting from left, traverse each element of array using for loop ( i=0;i&lt;n;i++ )</p></li><li><p>If current element is negative (&lt;0) make it positive by multiplying with -1.( arr[i]=arr[i]*-1 )</p></li><li><p>Add each element to the sum.</p></li><li><p>Return the sum as the maximum subset difference possible.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/hJE3MMhd" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;stdio.h&gt; int subsetDifference(int arr[], int n){ &nbsp; &nbsp;int sum = 0; &nbsp; &nbsp;for (int i = 0; i &lt; n; i++){ &nbsp; &nbsp; &nbsp; if(arr[i]&lt;0) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[i]=arr[i]*-1; &nbsp; &nbsp; &nbsp; sum += arr[i]; &nbsp; &nbsp;} &nbsp; &nbsp;return sum; } // Driver Code int main(){ &nbsp; &nbsp;int arr[] = { -1, 3, 5, 17, -32, 12 }; &nbsp; &nbsp;int n = 6; &nbsp; &nbsp;printf(&quot;Maximized difference between subsets : %d&quot;, subsetDifference(arr, n)); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Maximized difference between two subsets: 70</pre>
raja
Updated on 17-Aug-2020 08:32:06

Advertisements