Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Presentations Approaches to Conveying Information

Answered: 1 week ago

Question

LO1 Discuss four different views of motivation at work.

Answered: 1 week ago

Question

LO6 Summarize various ways to manage retention.

Answered: 1 week ago

Question

LO3 Define the difference between job satisfaction and engagement.

Answered: 1 week ago