Advance Recursion - CP Java-2048
Advance Recursion - CP Java-2048
Introduction
As we have already discussed what recursion is and how to implement it. In this
module, we are going to cover some other important concepts and problems based
on the recursion. Recursion is a very powerful tool and it makes our code readable
and short. But its applications are very vast therefore we will solve some more
problems so that we can come in handy.
Problem statement: You are given an array arr of N distinct integers. Your task
is to find all the non-empty subsets of the array.
Approach:
Using recursion we can create all the possible subsets, we can add the current
element to the temporary array, and make a recursive call for the next element,
when the function call returns we remove the current element from the temporary
array and again make a recursive call for the next element.
Each time we reach the end of the array, we will add the temporary array to the
answer.
1
Algorithm:
2
Code:
3
}
}
return 1;
});
System.out.println();
for(int i = 0; i < ans.size(); i++){
for(int j = 0; j < ans.get(i).size(); j++){
System.out.print(ans.get(i).get(j) + " ");
}
System.out.println();
}
}
}
Time Complexity: O( 2N )
Each element of the array has 2 options therefore overall 2N for N elements, Hence
the time complexity is O( 2N).
Since a total of 2(N-1) subsets are generated, and the maximum possible length of
each subset is of order N . Hence the space complexity is O(N * 2N).
Combination Sum
Problem statement: Your task is to find all unique combinations in the array
whose sum is equal to B. A number can be chosen any number of times from
array/list ARR.
4
Algorithm:
Code:
public static void combinationSum(int[] nums, int ind, int n,int target, ArrayList<Integer> temp){
// recursive call
//pick the ith StackTraceElement
temp.add(nums[ind]);
combinationSum(nums,ind,n,target-nums[ind],temp);
temp.remove(temp.size()-1);
combinationSum(nums,ind+1,n,target,temp);
}
5
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int target = sc.nextInt();
Arrays.sort(nums);
combinationSum(nums,0,n,target,temp);
}
}
Since we are recursing over all possible combinations of the array, the time
complexity will be O(2N).
The space complexity due to the recursion stack will be O(N). For each possible
state, we store all possible elements in temp. Hence, the overall space complexity
will be O(N*N).
6
Permutations of the string:
Problem statement:
You are given a string Str consisting of lowercase English letters. Your task is to
return all permutations of the given string in lexicographically increasing order.
Algorithm:
7
Code:
// recursive calls ?
int n = str.length();
for(int i=index;i<n;i++){
str = swap(str,i,index);
Permutations(str,index+1,ans);
8
str = swap(str,i,index);
}
}
Time Complexity: O(N! * log(N!)), Where N is the length of the given string.
We are also sorting the ans list of size O(N!) which will take O(N! * log(N!)) time.
Thus, the final time complexity is O(N! * log(N!) + N * N!) ~ O(N! * log(N!))
Space Complexity: O(N * N!), Where N is the length of the given string.
O(N) recursion stack is used by the recursive function, we are also storing the
permutations in a list which will O(N * N!) space. Thus, the final space complexity is
O(N + N * N!) ~ O(N * N!).