forked from AaqilSh/DSA-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindLongestBitonicSubarrayInAList.py
48 lines (33 loc) · 1.1 KB
/
FindLongestBitonicSubarrayInAList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from random import randrange
def findBitonicSublist(A):
n = len(A)
if n == 0:
return
end_index = 0
max_len = 1
i = 0
while i + 1 < n:
# check for the longest bitonic subarray starting at `A[i]`
# reset length to 1
length = 1
# run till sequence is increasing
while i + 1 < n and A[i] < A[i + 1]:
i = i + 1
length = length + 1
# run till sequence is decreasing
while i + 1 < n and A[i] > A[i + 1]:
i = i + 1
length = length + 1
# run till sequence is equal
while i + 1 < n and A[i] == A[i + 1]:
i = i + 1
# update longest bitonic subarray if required
if length > max_len:
max_len = length
end_index = i
# print the longest bitonic subarray
print("The length of the longest bitonic subarray is", max_len)
print("The longest bitonic subarray is", A[end_index - max_len + 1: end_index + 1])
if __name__ == '__main__':
A = [3, 5, 8, 4, 5, 9, 10, 8, 5, 3, 4]
findBitonicSublist(A)