
- 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 strictly increasing then decreasing sublist in Python
Suppose we have a list of numbers called nums. We have to find the length of the longest sublist such that (minimum length 3) its values are strictly increasing and then decreasing.
So, if the input is like nums = [7, 1, 3, 5, 2, 0], then the output will be 5, as the sublist is [2, 4, 6, 3, 1] is strictly increasing then decreasing.
To solve this, we will follow these steps −
- i := 0, n := size of a, res := -infinity
- while i < n - 2, do
- st := i
- linc := 0, ldec := 0
- while i < n - 1 and a[i] < a[i + 1], do
- linc := linc + 1
- i := i + 1
- while i < n - 1 and a[i] > a[i + 1], do
- ldec := ldec + 1
- i := i + 1
- if linc > 0 and ldec > 0, then
- res := maximum of res and (i - st + 1)
- while i < n - 1 and a[i] is same as a[i + 1], do
- i := i + 1
- return res if res >= 0 otherwise 0
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, a): i, n, res = 0, len(a), float("-inf") while i < n - 2: st = i linc, ldec = 0, 0 while i < n - 1 and a[i] < a[i + 1]: linc += 1 i += 1 while i < n - 1 and a[i] > a[i + 1]: ldec += 1 i += 1 if linc > 0 and ldec > 0: res = max(res, i - st + 1) while i < n - 1 and a[i] == a[i + 1]: i += 1 return res if res >= 0 else 0 ob = Solution() nums = [8, 2, 4, 6, 3, 1] print(ob.solve(nums))
Input
[[8, 2, 4, 6, 3, 1]
Output
5
- Related Articles
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Program to find length of contiguous strictly increasing sublist in Python
- Program to check whether list is strictly increasing or strictly decreasing 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 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
- Strictly increasing or decreasing array - JavaScript
- 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

Advertisements