Search code examples
javaarrayscombinationsbacktrackingrecursive-backtracking

Filling an array with all combinations of formula N^R


For a homework question i need to fill an array with all combinations of the formula N^R. The variable R is constant and is 6. The variable N is not constant and let's say it's 2. So 2^6 = 64. Now what i need is an array with all the combinations (in this case 64). I found a website which does exactly what i need and the output in this case should be:

[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0],
[0, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 0],
[1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 0, 0],
[1, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0],
[1, 1, 0, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 0, 1],
[1, 1, 0, 1, 1, 0],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1]

I have tried realising this with for loops, but without succes.

I don't want the full code of an algorithm that makes this possible, but I want to be helped on my way. Thanks in advance.


Solution

  • I've come up with this solution, which is a bit clumsy but should probably work for your case, the comments should explain everything:

    public static void printCombinations(int R, int N) {
        // calculate the combinations
        String[][] combinations = calculateCombinations(R, N);
        // iterate over all
        for (int i = 0; i < combinations.length; i++) {
            // prints the commas at the end
            if (i != 0) {
                System.out.println(',');
            }
            // print to std out
            System.out.print(Arrays.toString(combinations[i]));
        }
        System.out.println();
    }
    
    public static String[][] calculateCombinations(int R, int N) {
        // calculate our limit
        int limit = (int) StrictMath.pow(N, R);
        // create the result array
        String[][] result = new String[limit][R];
        // iterate over all possibilities
        for (int i = 0; i < limit; i++) {
            // convert to base
            String base = Long.toString(i, N);
            // holds our temporary value
            StringBuilder intermediate = new StringBuilder(R);
            // pad the value from the start with zeroes if needed
            for (int sub = R - base.length(); sub > 0; sub--) {
                intermediate.append('0');
            }
            // append our number
            intermediate.append(base);
    
            // append to result
            result[i] = intermediate.toString().split("");
        }
        // return the result
        return result;
    }
    

    And can then be called like this to pretty print it out:

    printCombinations(6, 2);
    

    Or to get it as a result:

    String[][] result = calculateCombinations(6, 2);
    

    Running Demo