diff --git a/data_structures/arrays/sliding_window.py b/data_structures/arrays/sliding_window.py new file mode 100644 index 000000000000..170c60796f57 --- /dev/null +++ b/data_structures/arrays/sliding_window.py @@ -0,0 +1,53 @@ +""" +Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k. + +https://www.geeksforgeeks.org/sum-minimum-maximum-elements-subarrays-size-k/ +""" + + +# this function will return sum of min and max of the window +def get_min_max(array: list, start: int, end: int) -> int: + max = min = array[start] + + for i in range(start + 1, end + 1): + if array[i] < min: + min = array[i] + if array[i] > max: + max = array[i] + + return max + min + + +def sum(array: list, size: int, k: int) -> int: + """ + Args: + array (list): the input array + size (int): size of the array + k (int): size of the sub-array + + Returns: + int : sum of the minimum and maximum elements of all sub-arrays of size-k + """ + """ + Examples: + >>> sum([2, 5, -1, 7, -3, -1, -2], 7 ,4) + 18 + """ + # create first window of size k + start, end = 0, k - 1 + result = 0 + # get the minimum and maximum element from the window and add it to the result + result += get_min_max(array, size, start, end) + + while start < size - k and end < size: + start += 1 + end += 1 + result += get_min_max(array, start, end) + + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod()