Find all combinations of group of numbers in an array on the fly

  • Thread starter Thread starter Silvers11
  • Start date Start date
S

Silvers11

Guest
Hello!

I have a little special problem I am cracking my head on how to do this. If it is possible to do. I have tried to assume some code on how it could look like to understand it better. I can try describing in steps. It might be easier: (To mention, this is a part of a micro-optimizing code. So this is the reason for this question)

.......................................................................................................

1. I have an array: "numbersArray" with 1000 numbers between: 0-1000.

2. Now I want to find all combinations of 2 numbers between 0-1000. For example: 0,1 is one combination, 0,2 is another etc. I will do this from 2-45 group of numbers. To find all combinations. "groupNumbers" holds numbers: 2-45.

3. "combinationsArray" is a 2 dimensional array. This array could for example store all combinations where the 1st dimension tells how many combinations and the numbers(combinations) are stored in the 2nd dimension.

Following the code. The 3 steps tells this.

.......................................................................................................

The thing is that I could find all combinations if I wrote some ugly code and stored all combinations in "combinationsArray". However, if I do that, this will take very long time as a "preperation" of work because I can have up to 100,000 indexes or more later in "numbersArray".

My question is then if it is possible to find all those combinations ON THE FLY so this whole process is only iterated once instead of storing all combinations FIRST and then iterate through them AGAIN which would be a double set of iterations.

Thank you

void findCombinationsofNumbers()
{
//Declaring the array with 1000 indexes(The number of indexes can differ and why I see if there is any dynamic solution like below?)
int[] numbersArray = new int[1000];
for (int i = 0; i < numbersArray.Length; i++)
{
numbersArray = i; //Assign number from 0-1000
}
//Declaring the array with 43 indexes
int[] groupNumbers = new int[43];
for (int i = 0; i < groupNumbers.Length; i++)
{
groupNumbers = i + 2; //Assign number from 2-45
}

/*-----------------------------------------------------*/
//Now we want to iterate this loop that holds integers between: 2-45 and in the first Iteration:
//1. Find all combinations of 2 integers in: "numbers".
//2. Find all combinations of 3 integers in: "numbers".
//3. Find all combinations of 4 integers in: "numbers".
//4. All up to all combinations of 45 integers in "numbers"
for (int i = 0; i < groupNumbers.Length; i++) //2-45
{
//So for this first iteration. We could for example have a 2 dimensional array(combinationsArray) with all combinations of 2 numbers from the "numbersArray" like:
int XnumberofcombinationsForThisGroup = 100; //??? We dont know how many combinations there are here?

//Every first dimension could then be one combination of 2 numbers stored in the 2nd dimension
int[,] combinationsArray = new int[XnumberofcombinationsForThisGroup, 2];

//Now iterate all combinations of 2 numbers from: "numbersArray"
for (int i2 = 0; i2 < combinationsArray.Length; i2++)
{
for (int i3 = 0; i3 < combinationsArray.GetLength(i2); i3++)
{
//Here comes one combination of numbers at a time. For the first groupNumbers, this would be all combinations of 2 numbers between: 2-45
//
//
//
//
//
}
}
}
}

Continue reading...
 
Back
Top