
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find length of longest contiguously strictly increasing sublist after removal in Python
Suppose we have a list of numbers called nums, we have to find the maximum length of a contiguous strictly increasing sublist. We are allowed to remove at most single element from the list.
So, if the input is like nums = [35, 5, 6, 7, 8, 9, 12, 11, 26], then the output will be 7, because if we remove 12 from nums, the list will be [5, 6, 7, 8, 9, 11, 26], the length is 7, this is the longest, contiguous, strictly increasing sub-list.
To solve this, we will follow these steps −
- if nums is empty, then
- return 0
- end := a list of size same as nums and fill with 1
- start := a list of size same as nums and fill with 1
- for i in range 1 to size of nums - 1, do
- if nums[i] > nums[i - 1], then
- end[i] := end[i - 1] + 1
- if nums[i] > nums[i - 1], then
- for j in range size of nums - 2 to 0, decrease by 1, do
- if nums[j + 1] > nums[j], then
- start[j] := start[j + 1] + 1
- if nums[j + 1] > nums[j], then
- res := maximum of the elements of end and element of start
- for k in range 1 to size of nums - 2, do
- if nums[k - 1] < nums[k + 1], then
- res := maximum of res and (end[k - 1] + start[k + 1])
- if nums[k - 1] < nums[k + 1], then
- return res
Example
Let us see the following implementation to get better understanding −
def solve(nums): if not nums: return 0 end = [1 for i in nums] start = [1 for i in nums] for i in range(1, len(nums)): if nums[i] > nums[i - 1]: end[i] = end[i - 1] + 1 for j in range(len(nums) - 2, -1, -1): if nums[j + 1] > nums[j]: start[j] = start[j + 1] + 1 res = max(max(end), max(start)) for k in range(1, len(nums) - 1): if nums[k - 1] < nums[k + 1]: res = max(res, end[k - 1] + start[k + 1]) return res nums = [35, 5, 6, 7, 8, 9, 12, 11, 26] print(solve(nums))
Input
[35, 5, 6, 7, 8, 9, 12, 11, 26]
Output
7
- Related Articles
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Program to find length of contiguous strictly increasing sublist in Python
- Program to find length of longest distinct sublist in Python
- Program to find length of longest increasing subsequence in Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find length of longest alternating inequality elements sublist in Python
- Program to find length of longest sublist with given condition in Python
- Program to find length of longest circular increasing subsequence in python
- Program to find length of longest consecutively increasing substring in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find length of longest consecutive sublist with unique elements in Python
- Program to find length of longest sublist with value range condition in Python
- Program to find length of longest contiguous sublist with same first letter words in Python
- Program to find length of longest sublist containing repeated numbers by k operations in Python
- Program to find all contiguously increasing numbers in start end range in Python

Advertisements