Below program is to find all the subsets of an array. Is time complexity of the program is O(2^n)?
Is there any easy way to find the time complexity of recursive function?
Thanks in anticipation
public static void getAllSubSet(int[] arr,int[] subset,int index){
if(index == arr.length)
printarr(subset);
else{
subset[index] = -1;
getAllSubSet(arr, subset, index+1);
subset[index] = arr[index];
getAllSubSet(arr, subset, index+1);
}
}
public static void printarr(int[] set){
for(int i=0;i<set.length;i++){
if(set[i] != -1){
System.out.print(set[i] +" ");
}
}
System.out.println("");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {1,2,3};
int[] subset = new int[arr.length];
getAllSubSet(arr, subset, 0);
}
O(2^n). It is impossible to do better thanΘ(n2^n)because that is the size of the output.