# 5 Best Ways to Find Length of Longest Strictly Increasing Sublist After Removal in Python

Rate this post

π‘ Problem Formulation: The task is to determine the length of the longest contiguously strictly increasing sublist from a given list, after the potential removal of one element. In essence, you can choose to remove one element to maximize the length of an increasing sequence. For example, given the input list `[10, 1, 3, 2, 5]`, the desired output is `3`, representing the length of the sublist `[1, 3, 5]` after removing the number `2`.

## Method 1: Brute Force Approach

This brute force method iteratively removes each element from the list and checks the longest increasing sublist that can be formed without that element. While not efficient for large lists due to its O(n^2) complexity, it is straightforward to understand and implement.

Here’s an example:

```def longest_increasing_sublist(arr):
def is_increasing(sublist):
return all(x < y for x, y in zip(sublist, sublist[1:]))

max_length = 0
for i in range(len(arr)):
# Remove the element at index i
temp = arr[:i] + arr[i+1:]
j = 0
# Test every subsequence of temp
for k in range(len(temp) + 1):
if is_increasing(temp[j:k]):
max_length = max(max_length, k - j)
else:
j = k
return max_length

example_list = [10, 1, 3, 2, 5]
print(longest_increasing_sublist(example_list))```

The output of this code snippet is `3`.

The code defines a helper function `is_increasing()` which checks whether a sublist is strictly increasing. Then, it iterates through the original list, removing one element at a time, and computes the length of the longest increasing subsequence without that element. Finally, it returns the maximum length found.

## Summary/Discussion

• Method 1: Brute Force Approach. The brute force approach is simple to understand and implement but is inefficient for large datasets due to its O(n^2) time complexity.
• Method 2: Dynamic Programming. This method is more efficient for larger datasets. It reduces the time complexity significantly, but requires a more intricate understanding of dynamic programming concepts and additional space.
• Method 3: Greedy Algorithm. Greedy algorithms are generally faster and can solve this problem efficiently with less complexity. However, they may require careful implementation to handle edge cases.
• Method 4: Divide and Conquer. Divide and conquer can optimize the performance in certain scenarios but might be overkill for this type of problem and can be complex to code correctly.
• Bonus One-Liner Method 5: Using List Comprehensions and max(). A one-liner is elegant and concise, suitable for coding interviews or if performance is not a concern. However, it might sacrifice readability and is not always as performant.