-
Notifications
You must be signed in to change notification settings - Fork 5
/
LongestSubarrayDiff_1.txt
64 lines (53 loc) · 1.95 KB
/
LongestSubarrayDiff_1.txt
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
n = list(map(int, input().strip().split(" ")))
size = n[0]
arr = n[1:]
def longestSubarray(A):
# Initialize a variable to store
# length of longest sub-array
res = []
maxLen = 0
beginning = 0
window = {}
start = 0
for end in range(len(arr)):
# Increment the count of that
# element in the window
if arr[end] in window:
window[arr[end]] += 1
else:
window[arr[end]] = 1
minimum = min(list(window.keys()))
maximum = max(list(window.keys()))
print(maximum)
# If the difference is not
# greater than X
if maximum - minimum <= 1:
# Update the length of the longest
# sub-array and store the beginning
# of the sub-array
if maxLen < end - start + 1:
maxLen = end - start + 1
beginning = start
# Decrease the size of the window
else:
while start < end:
# Remove the element at start
window[arr[start]] -= 1
# Remove the element from the window
# if its count is zero
if window[arr[start]] == 0:
window.pop(arr[start])
# Increment the start of the window
start += 1
# Find the maximum and minimum element
# in the current window
minimum = min(list(window.keys()))
maximum = max(list(window.keys()))
# Stop decreasing the size of window
# when difference is not greater
if maximum - minimum <= 1:
break
# Print the longest sub-array
for i in range(beginning, beginning + maxLen):
res.append(arr[i])
return len(res)