4
4
https://www.geeksforgeeks.org/sum-minimum-maximum-elements-subarrays-size-k/
5
5
"""
6
6
7
- #this function will return sum of min and max of the window
8
- def get_min_max ( array : list , start : int , end : int ) -> int :
9
-
7
+
8
+ # this function will return sum of min and max of the window
9
+ def get_min_max ( array : list , start : int , end : int ) -> int :
10
10
max = min = array [start ]
11
-
12
- for i in range (start + 1 , end + 1 ):
11
+
12
+ for i in range (start + 1 , end + 1 ):
13
13
if array [i ] < min :
14
14
min = array [i ]
15
15
if array [i ] > max :
16
16
max = array [i ]
17
-
18
- return max + min
19
17
20
- def sum (array :list ,size :int ,k :int ) -> int :
18
+ return max + min
19
+
20
+
21
+ def sum (array : list , size : int , k : int ) -> int :
21
22
"""
22
23
Args:
23
24
array (list): the input array
24
25
size (int): size of the array
25
26
k (int): size of the sub-array
26
-
27
+
27
28
Returns:
28
29
int : sum of the minimum and maximum elements of all sub-arrays of size-k
29
30
"""
@@ -33,19 +34,20 @@ def sum(array:list ,size:int ,k:int) -> int:
33
34
18
34
35
"""
35
36
# create first window of size k
36
- start , end = 0 , k - 1
37
+ start , end = 0 , k - 1
37
38
result = 0
38
- #get the minimum and maximum element from the window and add it to the result
39
- result += get_min_max (array ,size ,start ,end )
40
-
41
-
42
- while start < size - k and end < size :
39
+ # get the minimum and maximum element from the window and add it to the result
40
+ result += get_min_max (array , size , start , end )
41
+
42
+ while start < size - k and end < size :
43
43
start += 1
44
44
end += 1
45
- result += get_min_max (array ,start ,end )
46
-
45
+ result += get_min_max (array , start , end )
46
+
47
47
return result
48
48
49
- if __name__ == '__main__' :
49
+
50
+ if __name__ == "__main__" :
50
51
import doctest
51
- doctest .testmod ()
52
+
53
+ doctest .testmod ()
0 commit comments