Search code examples
javaalgorithmpseudocode

Getting all possible combinations of an array of objects


I have come to a sudden stop in the development of an application of mine.

I need to get all possible combinations of an array of Arguments, for instance the array could look like this

[Integer, Boolean, String]

The total possible combinations would then be 7 (2^X - 1, where X is the amount of arguments, a formula which me and a friend came up with while trying to tackle this problem)

Here's a visualization of the possible combinations.
[Integer, Boolean, String],
[Integer, Boolean],
[Integer, String],
[Integer],
[Boolean, String],
[Boolean] and
[String]

As you can see in the visualization the only thing which is necessary is that the entries always have the same order relative to each other (Integer must always be before Boolean and String, and Boolean must always be before String)

What I am asking is :
How can I find every possible combination of an array of Strings where the combinations are not limited to any specific length of the present entries but only limited to having the same order of the entries relative to each other?

If anyone could give me a push in the right direction it would be much appreciated. I have been looking at a few posts about finding every possible value but I could not find any post which was of any help to me.

If any further information about the problem is needed feel free to ask


Solution

  • Let me give you hint:

    Check binary presentation of decimal numbers:

    0 000
    1 001
    2 010
    3 011
    4 100
    5 101
    6 110
    7 111
    

    Now, lets arrange your combinations in this fashion:

    [_, _, _]
    [_, _, S]
    [_, B, _]
    [_, B, S]
    [I, _, _]
    [I, _, S]
    [I, B, _]
    [I, B, S]
    

    Next step is for you to implement N-bit number