The value 0 can be excluded from any such list, which is shown as the “- 1” part of “2**n – 1”.

The idea is that each binary position represents one of the elements in the array. If the first binary digit is a “1”, then the first element of the array is in that combination.

Assume the array contains ‘A’, ‘B’, ‘C’ and ‘D’. Then the binary number 0011 represents the combination ‘C’ and ‘D’. The number 1010 is ‘A’ and ‘C’. And 1111 is ‘A’, ‘B’, ‘C’ and ‘D’.

In short, just generate all binary numbers up to the limit of digits needed. This corresponds to powers of 2. Then grab the associated array elements.

Quick and easy.

Tom

]]>Let’s have the contents be A,B,C,D instead of names just to make this easier to type.

call the array ar so 4 elements ar1 =’A’ ar2=’B’ ar3=’C’ and ar4=’D’

N=4 the number of elements and K=2 the number of two combinations so we will get the result AB AC AD BC BD and CD

here are the for loops.

for x1 = 1 to N-1 note: n-1 is 3 in this case

for x2 = (x1+1) to N note: this is from 2 to 4

ar(x1)+ar(x2) = ‘A’+’B’ = ‘AB’

next

next

first time through x1=1 so you get A, and x2=1 so you get B ‘AB’ x2 then continues to increment so you then get ‘AC’ ‘AD’ ‘AE’ then x1 increments so you get ‘B’ then x2 gives you ‘BC’ then ‘BD’, then x1 increments and you get ‘C’ and x2 gives you ‘CD’

then you would have to do another set of for loops for k=3 and k=4 but since these loops are so similar you could probably put them in a subrouting and pass in the N and K values therby making it more generic.

hope this helps:

D wrka S 10 dim(10) incoming array D result S 100 dim(100) array to be loaded and tested for uniqueness numEl is position of first blank element - 1 (10 if no blank elements) I'm sure this could be similified with recursion but it's 10 little code snipets like this one. Then the number of unique entries is the first blank in result array -1 I think you need to sort wrka before you begin. *--------- process 3 elements C if numEl > 2 C for x = 1 to numEl - 2 C for y = 2 to numEl - 1 C for z = 3 to numEl C eval wrkPos = %lookup(*blanks: result) C C eval wrkStr = wrka(x) + wrka(y) + wrka(z) C if %lookup(wrkStr : result) = *zero C eval result(wrkPos) = wrkStr C endif C endfor C endif C endsr]]>

But I could not find anything Substantial from the link.

I think you missed reading one combination i.e.4C4 which is equal to 1.

Please any other ideas/help you can give..that would be great…

Thanks,

Karan

]]>http://www.codeguru.com/cpp/cpp/algorithms/combinations/article.php/c5117/ ]]>

Suppose we are having an array with names:

Karan

Julie

Mark

Robert

Since there are 4 elements in the array for my further requirement I need to have all the non-repetitive combination of these Four names.

Going with mathematical formula for Combination we have the following output for 4 elements i.e.

NC1 + NC2 + NC3 + NC4……..NCN or 2**n – 1

Which for our case comes out to be 15.

So there could be 15 combination of four names in the array.

Like if we consider all the 4 elements for Combination we have Karan+Julie+Mark+Robert as one Combination which is equivalent to 4C4.

And if we consider 3 elements for Combination we wil have 4 combinations:

Karan+Julie+Mark

Karan+Julie+Robert

Julie+ Mark+Robert

Karan + Mark + Robert

Which is equivalent to 4C3.

And if we consider 2 elements for Combination we wil have 6 combinations:

Karan+Julie

Julie+Robert

Mark+Robert

Julie + Mark

Karan + Mark

Karan + Robert

Which is equivalent to 4C2.

And if we consider 1 elements for Combination we wil have 4 combinations:

Karan

Julie

Mark

Robert

Which is equivalent to 4C1.

So Sum of all these combination comes out to be 15.

For this we need to have a generic logic for the combinations of N number of elements in the array.

Hope this will Help!!

]]>