public static void main(String[] arg) {

  int arr[] = {1, 6, 11, 5};
    int n = 4;
   System.out.println( "The minimum difference between two sets is "
          + findMin(arr, n));



  }

 public static int findMinRec(int arr[], int n, int sumCalculated, int sumTotal)
{
    // If we have reached last element.  Sum of one
    // subset is sumCalculated, sum of other subset is
    // sumTotal-sumCalculated.  Return absolute difference
    // of two sums.
    if (n==0)
        return Math.abs((sumTotal-sumCalculated) - sumCalculated);


    // For every item arr[i], we have two choices
    // (1) We do not include it first set
    // (2) We include it in first set
    // We return minimum of two choices
    return Math.min(findMinRec(arr, n-1, sumCalculated+arr[n-1], sumTotal),
                    findMinRec(arr, n-1, sumCalculated, sumTotal));
}

// Returns minimum possible difference between sums
// of two subsets
public static int findMin(int arr[], int n)
{
    // Compute total sum of elements
    int sumTotal = 0;
    for (int i=0; i<n; i++)
        sumTotal += arr[i];

    // Compute result using recursive function
    return findMinRec(arr, n, 0, sumTotal);
}

results matching ""

    No results matching ""