CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Set of Unique pairs

1. Member
Join Date
Jun 2019
Posts
30

## Set of Unique pairs

Hi,

I have chasing from last two days for the logic please help me out for the below problem.

Input : len =6,arr[]={1,2,3,4,5,6}
Output: (1,2)(3,4)(5,6) //1 set of pairs
(1,4)(2,6)(3,5)//second set of pair which is different from set 1
(1,4)(2,3)(5,6)//invalid
Generate set of all possible pairs
Sets should be unique and should not repetive

2. ## Re: Set of Unique pairs

Is it a homework?

3. Member
Join Date
Jun 2019
Posts
30

4. ## Re: Set of Unique pairs

Originally Posted by Raj90
Then have a look at this FAQ

5. Member
Join Date
Jun 2019
Posts
30

## Re: Set of Unique pairs

Well it's not kind of homework. I faced this question in interview.. I tried
Code:
```#include <stdio.h>

struct pairSet
{
int p1;
int p2;
};

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

for(i=0;i<len;i++)
{
for(j=1;j<len;j++)
{
ps[i].p1 = a[i];
ps[i].p2 = a[j];

printf("pairs : %d\t %d\n",ps[i].p1,ps[i].p2);
}
}
}```
Last edited by VictorN; January 8th, 2020 at 04:15 AM. Reason: added code tags

6. ## Re: Set of Unique pairs

2. Can you use STL classes?

7. Member
Join Date
Jun 2019
Posts
30

## Re: Set of Unique pairs

Ha actually i am trying in C... please give give me some input related to c

8. ## Re: Set of Unique pairs

OK, did you test your code? Does it create all the possible pair sets?

9. ## Re: Set of Unique pairs

Generate set of all possible pairs
Sets should be unique and should not repetive
Does the input set arr contain only unique values?

Are (1, 2) and (2, 1) considered unique or repetitive?

10. Member
Join Date
Jun 2019
Posts
30

unique

11. ## Re: Set of Unique pairs

Then consider:

Code:
```#include <stdio.h>

int main()
{
const int a[] = {1, 2, 3, 4, 5, 6};
const int len = sizeof(a) / sizeof(a[0]);

int total = 0;

for (int i = 0; i < len; ++i)
for (int j = 0; j < len; ++j)
if (j != i) {
printf("pairs : %d, %d\n", a[i], a[j]);
++total;
}

printf("Total number of pairs is %d\n", total);
}```
given that the elements of array a are unique. A unique pair is formed by taking each element of a in turn (loop i) and pairing it in turn with every element of a (loop j) except the element being considered in loop i (the if statement).

If the elements of a weren't unique or (1, 2) is considered the same as (2, 1) then additional logic would be required.

The output is:

Code:
```pairs : 1, 2
pairs : 1, 3
pairs : 1, 4
pairs : 1, 5
pairs : 1, 6
pairs : 2, 1
pairs : 2, 3
pairs : 2, 4
pairs : 2, 5
pairs : 2, 6
pairs : 3, 1
pairs : 3, 2
pairs : 3, 4
pairs : 3, 5
pairs : 3, 6
pairs : 4, 1
pairs : 4, 2
pairs : 4, 3
pairs : 4, 5
pairs : 4, 6
pairs : 5, 1
pairs : 5, 2
pairs : 5, 3
pairs : 5, 4
pairs : 5, 6
pairs : 6, 1
pairs : 6, 2
pairs : 6, 3
pairs : 6, 4
pairs : 6, 5
Total number of pairs is 30```

12. Member
Join Date
Jun 2019
Posts
30

## Combination of unique sets of pairs

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

Code:
```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
Last edited by 2kaud; January 8th, 2020 at 07:27 AM.

13. ## Re: Set of Unique pairs

If the output from the code in post #11 is not as required, then can you explain in more detail exactly what is required - as the code in post #11 provides all possible unique pairs given a unique array.
Last edited by 2kaud; January 8th, 2020 at 07:40 AM.

14. Member
Join Date
Jun 2019
Posts
30

## Re: Set of Unique pairs

If you observe post #12

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

output:
(1,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 since (1,6) is repeated in set 3
In this way have to find all valid possible sets

15. ## Re: Set of Unique pairs

Originally Posted by Raj90
If you observe post #12

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

output:
(1,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 since (1,6) is repeated in set 3
In this way have to find all valid possible sets
You are not stating the requirement for an output set. If you want output grouped into 3, then consider:

Code:
```#include <stdio.h>

int main()
{
const int a[] = {1, 2, 3, 4, 5, 6};
const int len = sizeof(a) / sizeof(a[0]);
const int numSet = 3;

int total = 0;

for (int i = 0; i < len; ++i)
for (int j = 0; j < len; ++j)
if (j != i) {
printf("(%d, %d)", a[i], a[j]);
if (++total % numSet == 0)
puts("");
}

printf("Total number of pairs is %d\n", total);
}```
which gives the output:

Code:
```(1, 2)(1, 3)(1, 4)
(1, 5)(1, 6)(2, 1)
(2, 3)(2, 4)(2, 5)
(2, 6)(3, 1)(3, 2)
(3, 4)(3, 5)(3, 6)
(4, 1)(4, 2)(4, 3)
(4, 5)(4, 6)(5, 1)
(5, 2)(5, 3)(5, 4)
(5, 6)(6, 1)(6, 2)
(6, 3)(6, 4)(6, 5)
Total number of pairs is 30```
If something different is required, then you need to explain in much more detail the requirement - and not just repeat the given output example.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•