-2

EDIT: Please note, this question is not the same as "Generating all Possible Combinations" Im not looking for a simple Cartesian Product

I need to write a function that produces array of all possible pair combinations of 2 given arrays. So for example:

  1. a, b, c ...
  2. x, y ...

I need to find all pair combinations of these numbers, so it will look like:

  • Combination 1: a-x, b-y, c
  • Combination 2: a-y, b-x, c
  • Combination 3: a-x, c-y, b
  • Combination 4: a-y, c-x, b
  • and so on....

The result should be List of List of Pair, also Pair can contain only 1 number, if initial sets are not equal

thanks

-alex

alex
  • 33
  • 4
  • 1
    Should 'a-x' and 'x-a' be a different combination in your application? – Max Oct 07 '14 at 11:40
  • @sriram-sakthivel excuse me, but I dont see how my question duplicates the one you posted, Im not looking for a simple cartesion product, I need a list of unique combinations, where combination is a list of pairs – alex Oct 07 '14 at 12:34
  • @Max yes, but its not necessary since i can swap arrays and run it again. Also a-x is not a combination, combination is a full set of pairs: a-x, b-y, c – alex Oct 07 '14 at 12:35
  • I reopened the question, It will be better to add your attempt to the question. – Sriram Sakthivel Oct 07 '14 at 12:48
  • Is a-x, b, c, y possible? – David Eisenstat Oct 07 '14 at 16:09

1 Answers1

2

Let the arrays be A and B. Assume that A is not smaller than B and that every element in B must be paired. For each permutation A' of A, generate a pairing where A'[0] is paired with B[0], and A'[1] is paired with B[1], and ..., and the leftover elements of A' are unpaired.

Community
  • 1
  • 1
David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • Thanks, Its what I did, I paired array A to B with corresponding index, then at the end of each permutation cycle I rotated arrays, to get new combination – alex Oct 08 '14 at 09:51