On May 14, 4:51 am, divya <[email protected]> wrote:
> Algorithm to partition set of numbers into two s.t. diff bw their
> sum is min and they hav equal num of elements

void part(const int a[], int n_a, int g1[], int g2[])
  {
    int i, j, k;

    /* diff = sum(g1) - sum(g2) */
    int diff;

    sort(a, n_a);

    diff = 0;
    for (i = 0, j = 1, k = 0; j < n_a; ++k, i += 2, j += 2)
      {
        if ((a[i] > a[j]) == (diff > 0))
          {
            g1[k] = a[j];
            g2[k] = a[i];
          }
        else
          {
            g1[k] = a[i];
            g2[k] = a[j];
          }
        diff += g1[k] - g2[k];
      }
  }

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to