Question
Q. The following code contains recursion for longest increasing subsequence. I am confused with how recursion res = _lis(arr,i) works in _lis(int arr[], int n)
Q. The following code contains recursion for longest increasing subsequence.
I am confused with how recursion "res = _lis(arr,i)" works in _lis(int arr[], int n) method.
How does this recursion work in for loop and lead to the answer?
Class LIS
{
static int max_ref;
static int_lis(int arr[], int n){
if(n==1)
return 1;
in t res,max_ending_here=1;
for (int i=1; i < n; i++) {
res=_lis(arr,i);
if(arr[i-1]max_ending_here)
max_ending_here = res+1;
}
if (max_ref < max_ending_here)
max_ref = max_ending_here;
return max_ending_here;
}
static int lis(int arr[], int n)
{
max_ref = 1;
_lis( arr, n);
return max_ref;
}
public static void main(String args[])
{ int arr[] = { 3,4,-1,0,6,2,3};
int n = arr.length;
System.out.println("Length of lis is "
+ lis(arr, n) + "n");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started