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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!