I have an array with 6 items and I need to generate all permutations of lengths 4, 5, and 6. For example, if my array has a, b, c, d,e, f then

permutations of length 4 would be:

pemutations of length 5 would be:

and so on.

I know how to generate all permutations of length 6, which would use all the items in the array, but am not sure how to do if choosing lengths 4 and 5. Any help is appreciated. thanks.

If you tell me how you generate the permutations of length 6 (C# code) , I tell you how to do so with 4 and 5


I have a code which uses integer array. Hope this will help you.

using System;

namespace adc
    public class Permu
        static void permut(int k, int n, int[] nums)
            int i, j, tmp;

            /* when k > n we are done and should print */
            if (k <= n)

                for (i = k; i <= n; i++)
                    tmp = nums[i];
                    for (j = i; j > k; j--)
                        nums[j] = nums[j - 1];
                    nums[k] = tmp;

                    /* recurse on k+1 to n */
                    permut(k + 1, n, nums);

                    for (j = k; j < i; j++)
                        nums[j] = nums[j + 1];
                    nums[i] = tmp;
                for (i = 1; i <= n; i++)
                    Console.Write("{0} ", nums[i]);

        static void Main()
            int iCount;
            int[] rgNum = new int[100];
            int i;

            Console.Write("Enter n: ");
            iCount = int.Parse(Console.ReadLine());

            /* create a workspace of numbers in their respective places */
            for (i = 1; i <= iCount; i++)
                rgNum[i] = i;

            permut(1, iCount, rgNum);


I have figured out how to generate the permutations of the variable lengths.

The other issue I am having right now is hashing a hex string. When I get a permutation I get it as a string. For example,

abc would be a permutation. I need to have it as byte values, so the permutation needs to be 0a0b0c. Then I need to hash the hex string 0a0b0c as a hex string and not a regular text string.

Thanks for the reply. I tried looking at the link you gave, but this does not do what I need.

Let me try and explain it better. Lets say I have integers 1 2 3. I need to find the permutations, store each digit in the permutation as a byte value (possibly in a byte array), and then hash the byte array as a hex string.

For example,

one permutation would be "321". This would need to be stored as 030201. I would need to hash 030201 as a hex string, not a regular text string.

I am not sure how to go about doing this. I understand how to get all the permutations, but now am stuck at this point. I am just trying to automate something for testing purposes. thanks.

Thanks for the reply. I now think I am very close and have one small problem. To generate my permutations I am using the iList<T> interface. I feed it a byte array with values and get back the permutations as an iList. From the iList I can take each permutation and convert it to a string. What I need is to take each permutation and put it in a byte array without going to the string. I have not been able to get this to work. I can use StringBuilder and go from the iList to a string for each permutation, but this is not what I need since I need to hash the original permutation as a byte array.

For example:
If my input byte array is {00,01,02,03} then I get an iList back with all the permutations. One possible permutation is 01020300 and if I take this permutation out of the iList and convert it to a string using StringBuilder I get 1230 which is not what I need. I need the permutation from the iList to show up as a byte array with {01,02,03,00}. Then I can hash the byte array as I need to. Any ideas how I can go about doing this with my current implementation? Thanks.

Thanks for your help. I was able to figure it out.