- Related Questions & Answers
- 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 check whether list is strictly increasing or strictly decreasing in Python
- Program to find length of longest circular 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
- Strictly increasing or decreasing array - JavaScript
- Program to find length of longest sublist whose sum is 0 in Python
- Program to length of longest increasing path in a given matrix in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest consecutive sequence in Python
- Program to find length of longest palindromic substring in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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))

[[8, 2, 4, 6, 3, 1]

5

Advertisements