dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: Set of Unique pairs

  1. #1
    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

    Thanks in advance

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: Set of Unique pairs

    Is it a homework?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2019
    Posts
    30

    Re: Set of Unique pairs

    ya its kind of task

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: Set of Unique pairs

    Quote Originally Posted by Raj90 View Post
    ya its kind of task
    Then have a look at this FAQ
    Victor Nijegorodov

  5. #5
    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. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: Set of Unique pairs

    1. Please, edit your post adding CODE tags around your code snippets. Otherwise your code is unreadable.
    2. Can you use STL classes?
    Victor Nijegorodov

  7. #7
    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. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: Set of Unique pairs

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

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,971

    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?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  10. #10
    Join Date
    Jun 2019
    Posts
    30

    Re: Set of Unique pairs

    unique

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,971

    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
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  12. #12
    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");
    }
    I have tried for set 1. can you please help me out for remaining sets
    find set of all possible pairs
    Last edited by 2kaud; January 8th, 2020 at 07:27 AM.

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,971

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  14. #14
    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. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,971

    Re: Set of Unique pairs

    Quote Originally Posted by Raj90 View Post
    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

Page 1 of 3 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)