-6

how can we find out different combination of the elements of an array using c# code. are there any inbuilt library function for this.?

for eg: suppose an array has elements {2,3,4,5,6,7} then the possible combination would be 2,3,4,5,6,7,2 3,2 3 4,2 3 4 5, etc

so basically wat i need is a function which gives different combination based on its input for eg: comb(array,2) gives output 2 3,1 2,3 4 and comb(array,3) gives output 1 2 3,2 3 4,3 4 5 and so on

Eg: valid comnbination for array= {1, 2, 3} and length = 2 are 1 2,1 3,2 3 .....

iJade
  • 23,144
  • 56
  • 154
  • 243
  • 3
    What combinations do you want? – Albin Sunnanbo Oct 04 '11 at 06:11
  • What do you mean by "combination"? Do you mean all "combinations"where elements in a different order are considered the same and all elements of the array are "combined" as their own arrays? – Joshua Enfield Oct 04 '11 at 06:13
  • What do you actually mean with "different combination"? – Sai Kalyan Kumar Akshinthala Oct 04 '11 at 06:13
  • 2
    Are you looking for permutations? http://stackoverflow.com/questions/1272828/getting-all-the-permutations-in-an-array – Jon Skeet Oct 04 '11 at 06:13
  • 2
    (Downvoted for lack of clarity, by the way. If you clarify the question, I'll happily remove the downvote. At the moment, the question can't really be answered with any confidence that the answer will satisfy what you're actually trying to do.) – Jon Skeet Oct 04 '11 at 06:14
  • i have updated my Question plz check – iJade Oct 04 '11 at 06:15
  • Your example doesn't make any sense for me. Can you explain how did you get that 'combination'? – Piotr Auguscik Oct 04 '11 at 06:16
  • so basically wat i need is a function which gives different combination based on its input for eg: comb(array,2) gives output 2 3,1 2,3 4 and comb(array,3) gives output 1 2 3,2 3 4,3 4 5 and so on – iJade Oct 04 '11 at 06:16
  • so basically wat i need is a function which gives different combination based on its input for eg: comb(array,2) gives output 2 3,1 2,3 4 and comb(array,3) gives output 1 2 3,2 3 4,3 4 5 and so on – iJade Oct 04 '11 at 06:19
  • updated my question...plz be humble enough 2 check it once again.... – iJade Oct 04 '11 at 06:22
  • @Jay: Okay, so you want all possible permutations *of a given length*. (The question could still be clearer, to be honest...) Um, hang on - the examples you've given have all kept the element order - is that a requirement too? Or is "3 1 2" a valid combination? – Jon Skeet Oct 04 '11 at 06:25
  • no element order is not required......ya 3 1 2 is a valid combination.. – iJade Oct 04 '11 at 06:39
  • if 3 1 2 is a valid one, then I'm lost :) – Adrian Iftode Oct 04 '11 at 06:43
  • Jay, no offence, usually when someone is working on an algorithm he/she needs first to be able to produce "on paper" a solution for a given input. If the words are not enough to describe the problem, an example of input/output should be enough. So please update your question, for the following input: array= {1, 2, 3} and length = 2 what are all the possible outputs you're expecting – Adrian Iftode Oct 04 '11 at 06:53
  • Here r the valid combinations 1 2,1 3,2 3 ....and i'm sorry for the above 3 1 2 combination..its nt valid...i was confused... – iJade Oct 04 '11 at 07:33

2 Answers2

2
static void Main()
{
    var cnk = comb(new [] {1,2,3},2);
    foreach ( var c in cnk)
    {
    }
}

public static IEnumerable<int[]> comb(int[] a, int k)
{
    if (a == null || a.Length == 0 || k < 1 || k > a.Length)
        yield break;

    int n = a.Length;   
    // 1
    if ( k == 1)
        for ( int i = 0; i < n; i++)
        {   
            yield return new int[] {a[i]};
        }
    else
        {
            // k
            for ( int i = 0; i < n - k + 1; i++)
            {
                var res = new int[k];
                    for (int t = i, c = 0; t < i + k - 1; t++, c++)                 
                        res[c] = a[t];              
                for (int j = i + k - 1; j < n; j++)
                {                                                               
                    res[k-1] = a[j];                    
                    yield return res;
                }
            }
        }
}

You should take the algorithm from here, my answer doesn't solve your problem Algorithm to return all combinations of k elements from n

Community
  • 1
  • 1
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
0

Seemed logic is not absolutely correct as:

var cnk = comb(new[] { 1, 2, 3, 4 }, 3);

This gives 3 variants, but as a matter of fact it is 4:

1 2 3
1 2 4
1 3 4
2 3 4

I guess comb is better to be implemented in recursive way.

Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
Antonio M
  • 11
  • 3