Program to find length of contiguous strictly increasing sublist in Python

Suppose we have a list of numbers called nums, we have to find the maximum length of a contiguous strictly increasing sublist when we can remove one or zero elements from the list.

So, if the input is like nums = [30, 11, 12, 13, 14, 15, 18, 17, 32], then the output will be 7, as when we remove 18 in the list we can get [11, 12, 13, 14, 15, 17, 32] which is the longest, contiguous, strictly increasing sublist, and its length is 7.

Algorithm

To solve this, we will follow these steps ?

  • n := size of nums

  • pre := a list of size n and fill with 1s (stores length of increasing sequence ending at each position)

  • for i in range 1 to n ? 1, do

    • if nums[i] > nums[i ? 1], then

      • pre[i] := pre[i ? 1] + 1

  • suff := a list of size n and fill with 1s (stores length of increasing sequence starting at each position)

  • for i in range n ? 2 to 0, decrease by 1, do

    • if nums[i]

      • suff[i] := suff[i + 1] + 1

  • ans := maximum value of maximum of pre and maximum of suff

  • for i in range 1 to n ? 2, do

    • if nums[i ? 1]

      • ans := maximum of ans and (pre[i ? 1] + suff[i + 1])

  • return ans

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, nums):
        n = len(nums)
        if n <= 1:
            return n
            
        # pre[i] stores length of increasing sequence ending at position i
        pre = [1] * n
        for i in range(1, n):
            if nums[i] > nums[i - 1]:
                pre[i] = pre[i - 1] + 1
        
        # suff[i] stores length of increasing sequence starting at position i
        suff = [1] * n
        for i in range(n - 2, -1, -1):
            if nums[i] < nums[i + 1]:
                suff[i] = suff[i + 1] + 1
        
        # Maximum length without removing any element
        ans = max(max(pre), max(suff))
        
        # Try removing each element and check if we can connect sequences
        for i in range(1, n - 1):
            if nums[i - 1] < nums[i + 1]:
                ans = max(ans, pre[i - 1] + suff[i + 1])
        
        return ans

# Test the solution
ob = Solution()
nums = [30, 11, 12, 13, 14, 15, 18, 17, 32]
print(ob.solve(nums))

The output of the above code is ?

7

How It Works

The algorithm works in three phases:

  1. Forward pass: Calculate the length of strictly increasing sequence ending at each position

  2. Backward pass: Calculate the length of strictly increasing sequence starting at each position

  3. Combine sequences: For each position, check if removing that element allows us to connect the left and right increasing sequences

For the example [30, 11, 12, 13, 14, 15, 18, 17, 32], removing element 18 at index 6 connects the sequences [11, 12, 13, 14, 15] and [17, 32], giving us the longest increasing sublist of length 7.

Conclusion

This dynamic programming approach efficiently finds the maximum length of a contiguous strictly increasing sublist by allowing removal of at most one element. The algorithm runs in O(n) time complexity with O(n) space complexity.

Updated on: 2026-03-25T10:51:42+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements