I wrote this code to form combinations of a 16 bit binary number (I know it is in output is string format, but it is my goal to just print all possible 16 bit binary numbers). My question is, can this code be optimized? How can I calculate the time complexity of this code?public class Combinations {    static int len;    public static void formCombinations(StringBuilder start,String[] possible)    {        if(start.length()==len)        {            System.out.println(start);            return;        }        for(int i=0;i<possible.length;i++)        {               formCombinations(start.append(possible[i]), possible);            start.deleteCharAt(start.length()-1);        }    }    public static void formCombinations(int len,String[] a) {        Combinations.len=len;        formCombinations(new StringBuilder(), a);    }    public static void main(String[] args) {        String a[]={0,1};        formCombinations(16, a);    }}
Forming combinations of a 16 bit binary number
java;performance;recursion
Give your operators some breathing space, e.g.:for (int i = 0; i < possible.length; i++)possible is not very good name (it implies that something is impossible. I recommend alphabet instead.possible never changes. It makes it a good candidate for a final class member. Passing it down recursively wastes some stack space.I don't think overloading formCombinations is warranted.I see nothing obviously wrong performance wise. After all, you generate all permutations, and you generate each permutation exactly once. Of course, StringBuilder may impose some hidden penalties; Java experts would give an authoritative answer.In any case, an iterative solution requires less space, doesn't need a StringBuilder, and may serve as an iterator (in a sense of supporting has_next() and next() methods). I highly recommend implementing it: it is simple and instructive. Hint: think of next permutation as adding 1 to a binary number.