# 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

Live Demo

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

Updated on: 19-Nov-2020

344 Views