what if i use brute force to generate any possible combination of binary numbers as an array? closed

First of all, you need to provide a minimal reproducible example of your code. You can check here: https://stackoverflow.com/help/minimal-reproducible-example

In regards to your question, using three loops can be a solution:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        var combinations = new ArrayList<int[]>();
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    combinations.add(new int[] {i, j, k});
                }
            }
        }
    }
}

You can print them like this:

for (var c : combinations) 
{
    System.out.printf("{%d,%d,%d}%n", c[0], c[1], c[2]);
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top