0

I'm programming this in Java but don't worry about the language you choose to respond in. This is more of a logical question.

I've got an array of size n of items say: [a, b, c...]. I've got a second empty array of size p. Note that the empty array of size p will always be larger than the previous array size n. I want to iterate over all the combinations of placements of elements from the first array into the empty array. (Note that items in the populated array will always be in that order. The order they come in cannot change; however, the space between element placing can change.

Examples of combinations are (assume n=3 and p=5):

n = 3 = [a, b, c]
could make:
[a,b,c,_,_]
[a,b,_,c,_]
[a,b,_,_,c]
[a,_,b,c,_]
[a,_,b,_,c]
[a,_,_,b,c]
[_,a,b,c,_]
etc...

I know that I would start by shifting the last element all the way to the end 1 by 1 then shifting the second element over by 1 and repeating the shift of the last element until the second last element is at the end as well and thus requiring the third and final element to be shifted over once and repeat.

The problem I'm having is representing this in code. The sizes of the arrays are variables and not known to me but I know for a fact that n < p. I don't need the number of combinations it can make. I would like to have code that gives me the iteration to make the combinations so I can do further checks. If anyone could help me represent this in code, it would be extremely helpful.

Cam J
  • 21
  • 5
  • Read [Algorithm to return all combinations of k elements from n - Stack Overflow](https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n?rq=1) (but should it be shown in the "similar questions" already?) – user202729 Jan 06 '21 at 04:19
  • @user202729 I actually had read it. I, personally, couldn't find anything that pertained to my case. – Cam J Jan 06 '21 at 19:52
  • But there's. Each set of indices correspond to a way to fill in the empty spaces (just fill it sequentially to the indices in the array) – user202729 Jan 07 '21 at 01:38

0 Answers0