Skip to main content
Participant
April 13, 2023
Question

Error with longest increasing subsequence code

  • April 13, 2023
  • 1 reply
  • 239 views

I'm having trouble with a code I'm writing to find the longest increasing subsequence in an array. I've been following a tutorial from this resource, but my code won't run. I'm getting an error message saying "IndexError: list index out of range", and I'm not sure why. Here's the code I'm trying to use:

def LIS(arr):
n = len(arr)

# Declare the list (array) for LIS and initialize LIS
# values for all indexes
lis = [1]*n
# Compute optimized LIS values in bottom up manner
for i in range (1 , n):
for j in range(0 , i):
if arr[i] > arr[j] and lis[i]< lis[j] + 1 :
lis[i] = lis[j]+1
# Initialize maximum to 0 to get the maximum of all
# LIS
maximum = 0

# Pick maximum of all LIS values
for i in range(n):
maximum = max(maximum , lis[i])

return maximum

# Driver program to test above function
arr = [10, 22, 9, 33, 21, 50, 41, 60]
print("Length of lis is", LIS(arr))

Any help to figure out what's causing this error would be greatly appreciated. Thanks!

    This topic has been closed for replies.

    1 reply

    Adobe Employee
    April 19, 2023

    Hi @ashnaaa29387719lom8,

     

    Thank you for contacting Adobe Learning Manager Team.

    May I know which Adobe Product you use? We will try our best to help you once we know which product you use.

     

    Hope to hear from you soon.

     

    Regards,
    Debiprasad Maharana
    Adobe Inc.