maths.max_sum_sliding_window

Given an array of integer elements and an integer ‘k’, we are required to find the maximum sum of ‘k’ consecutive elements in the array.

Instead of using a nested for loop, in a Brute force approach we will use a technique called ‘Window sliding technique’ where the nested loops can be converted to a single loop to reduce time complexity.

Attributes

array

Functions

max_sum_in_array(→ int)

Returns the maximum sum of k consecutive elements

Module Contents

maths.max_sum_sliding_window.max_sum_in_array(array: list[int], k: int) int

Returns the maximum sum of k consecutive elements >>> arr = [1, 4, 2, 10, 2, 3, 1, 0, 20] >>> k = 4 >>> max_sum_in_array(arr, k) 24 >>> k = 10 >>> max_sum_in_array(arr,k) Traceback (most recent call last):

ValueError: Invalid Input >>> arr = [1, 4, 2, 10, 2, 13, 1, 0, 2] >>> k = 4 >>> max_sum_in_array(arr, k) 27

maths.max_sum_sliding_window.array