I have return a below code in java to produce the possible binary representation of n digits.
public List<String> binaryRepresenation(int n){ List<String> list = new ArrayList<>(); if(n>0){ permuation(n, list, ""); } return list; } private void permuation(int n, List<String> list, String str){ if(n==0){ list.add(str); }else{ permuation(n-1, list, str+"0"); permuation(n-1, list, str+"1"); } } For n=3, it produces 001 001 010 011 100 101 110 111 combinations. Overall this function produces 2^n possible representations.
Is it safe to say the time complexity is n * 2^n
Where 2^n time the method is called for base cases. In order to reach the each base case, it would have called the permutation method for maximum of n times.
So overall upper bound time complexity is n*2^n? Kindly correct me if i am wrong. I came to this conclusion based on the string permutation time complexity discussed in this thread Time Complexity of Permutations of a String . Your help will be much appreciated.