# Generate set of pairs of an array

#### satti15790

Joined Jul 17, 2017
17
Hi i am trying to implement the logic for the below issue... can you please help me out

input: len = 6, array[]={1,2,3,4,5,6}

output1,2)(3,4)(5,6)//set 1
(1,4)(2,6)(3,5)//set 2
(1,6)(4,5)(2,3)//set 3
(1,6)(5,3)(4,2)//set 4 invalid
so on

struct pairSet
{
int p1;
int p2;
};

int main()
{
int a[]={1,2,3,4,5,6};
int i;
int j,k;
int count=0;
struct pairSet ps[6];
int len = sizeof(a)/sizeof(a[0]);

printf("Set 1\n");
for(i=0;i<len
{
for(j=i+1;j<len;j++)
{
ps[count].p1 = a;
ps[count].p2 = a[j];
//printf("(%d,%d)\n",a,a[j]);
break;
}
count++;
i=i+2;
}
for(k=0;k<count;k++)
{
printf("(%d,%d) ",ps[k].p1,ps[k].p2);
}
printf("\n");
}
find set of all possible pairs

#### WBahn

Joined Mar 31, 2012
25,115
If you are just trying to generate all possible pairs, why is Set 4 invalid? It's one of the possible pairings. isn't it?

What are the rules for the sets you are generating? It might be helpful for us to know that information.

#### ci139

Joined Jul 11, 2016
1,093
those things might be tricky to implement indeed . . . your goal is $$\displaystyle{\frac{6!}{2!·4!}=\frac{5·6}{2!}=15}$$
its about swapping the members of the subsets in a way that cycles through all combinations
? is the set 4 invalid because the element sort order . . . ((too easy for not to mechanically induce the formula))
.
12 34 56 - 12 35 46 - 12 36 54
13 24 56 - 13 25 46 - 13 26 54
14 32 56 - 14 35 26 - 14 36 52
15 34 26 - 15 32 46 - 15 36 24
16 34 52 - 16 35 42 - 16 32 54
.
Edit : very bad non-optimized code doing ◤ that : https://onlinegdb.com/Hk0hjxExL -- there might be better data structures for such // sorting the pairs not implemented
credits : https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm -- i don't C , only some javaScript

Last edited:

#### djsfantasi

Joined Apr 11, 2010
5,969
What are the rules for the sets you are generating? It might be helpful for us to know that information.
I agree. I am assuming that a digit cannot appear more than once in a set? Is member (1,2) the same as (2,1)? I.e., is order insignificant? Or significant? Does this explain why set 4 is invalid? Must each set contain three members. And so on...

#### ci139

Joined Jul 11, 2016
1,093
there are load of online c interpreters . . . just took a look at your code in one of them https://onlinegdb.com/HJzoXKXgU
? it looks fine for what it does

#### ApacheKid

Joined Jan 12, 2015
140
Please give example using the smallest possible input set, then the next smallest input set and perhaps the next after that.

For example what is the output from a single value input?

What is the output from two values input, then three values?

e.g.

(7) -> ??

(23, 9) -> ??

(11, 22, 6) -> ??

Thanks

#### satti15790

Joined Jul 17, 2017
17
If you are just trying to generate all possible pairs, why is Set 4 invalid? It's one of the possible pairings. isn't it?

What are the rules for the sets you are generating? It might be helpful for us to know that information.
Set 4 is invalid because (1,6) is already present in Set 3 .
There are no particular rule for the generating the sets.

#### satti15790

Joined Jul 17, 2017
17
those things might be tricky to implement indeed . . . your goal is $$\displaystyle{\frac{6!}{2!·4!}=\frac{5·6}{2!}=15}$$
its about swapping the members of the subsets in a way that cycles through all combinations
? is the set 4 invalid because the element sort order . . . ((too easy for not to mechanically induce the formula))
.
12 34 56 - 12 35 46 - 12 36 54
13 24 56 - 13 25 46 - 13 26 54
14 32 56 - 14 35 26 - 14 36 52
15 34 26 - 15 32 46 - 15 36 24
16 34 52 - 16 35 42 - 16 32 54
.
Edit : very bad non-optimized code doing ◤ that : https://onlinegdb.com/Hk0hjxExL -- there might be better data structures for such // sorting the pairs not implemented
credits : https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm -- i don't C , only some javaScript
no pair should repeat as u mentioned in output pairs are repeating
like 56 46 etc

Last edited:

#### WBahn

Joined Mar 31, 2012
25,115
Set 4 is invalid because (1,6) is already present in Set 3 .
There are no particular rule for the generating the sets.
How can there be no particular rule for generating the sets if you just gave a (rather non-obvious) rule about sets being invalid if they contain a pair that appears in any prior set of pairs?

#### WBahn

Joined Mar 31, 2012
25,115
no pair should repeat as u mentioned in output pairs are repeating
like 56 46 etc
And here's another rule.

How many other rules are we going to find out about piecemeal?

What are the rules that define what is and what is not a valid set of pairs?

#### ApacheKid

Joined Jan 12, 2015
140
Set 4 is invalid because (1,6) is already present in Set 3 .
There are no particular rule for the generating the sets.
If there are no rules for generating the sets then we cannot devise an algorithm because an algorithm is rules.

#### ci139

Joined Jul 11, 2016
1,093
O'boy~O'boy~O'boy - non-repetitious assumedly sorted pairs . . .
all possible pairs .numberOf = Σ 1 to 5 = n(n+1)/2=15
12 13 14 15 16
...... 23 24 25 26
............. 34 35 36
.................... 45 46
.......................... 56
12 34 56 // 15-3=12 left -- this thing seems bifurcating ? doesn't it
13 45 26 -or- 13 46 25 // 12-3=9 left
14 36 25 -or- 14 35 26 // 9-3=6 left
15 23 46 -or- 15 24 36 // 6-3=3 left
16 24 35 -or- 16 23 45 // 3-3= done
out of options