From d566a5271b7aee74a6fe669982231ade376e714c Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 8 Oct 2020 21:55:55 +0530 Subject: [PATCH 1/7] Add files via upload --- other/max_sum_sliding_window.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 other/max_sum_sliding_window.py diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py new file mode 100644 index 000000000000..c938ff3a3f79 --- /dev/null +++ b/other/max_sum_sliding_window.py @@ -0,0 +1,43 @@ +""" +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. +""" +import sys +from typing import List + + +def max_sum(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(arr, k) + 24 + >>> k = 10 + >>> max_sum(arr,k) + Invalid input + -1 + """ + if len(array) < k or k < 0: + print("Invalid input") + return -1 + max_sum = -sys.maxsize + current_sum = sum(array[:k]) + for i in range(len(array) - k): + current_sum = current_sum - array[i] + array[i + k] + current_sum = max(max_sum, current_sum) + return current_sum + + +if __name__ == "__main__": + from random import randint + from doctest import testmod + + testmod() + array = [randint(-1000, 1000) for i in range(100)] + k = randint(0, 110) + print(f"The maximum sum of {k} consecutive elements is {max_sum(array,k)}") From 3fc8d30fdfed3a91c50266f57db0c3e07f0bba5d Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 8 Oct 2020 22:05:36 +0530 Subject: [PATCH 2/7] Update max_sum_sliding_window.py --- other/max_sum_sliding_window.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py index c938ff3a3f79..852e8f05d502 100644 --- a/other/max_sum_sliding_window.py +++ b/other/max_sum_sliding_window.py @@ -34,8 +34,8 @@ def max_sum(array: List[int], k: int) -> int: if __name__ == "__main__": - from random import randint from doctest import testmod + from random import randint testmod() array = [randint(-1000, 1000) for i in range(100)] From 9cc383c72385928948c7cc47f29f954df50f0064 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 9 Oct 2020 12:19:32 +0530 Subject: [PATCH 3/7] Update max_sum_sliding_window.py --- other/max_sum_sliding_window.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py index 852e8f05d502..9117e69c19fe 100644 --- a/other/max_sum_sliding_window.py +++ b/other/max_sum_sliding_window.py @@ -19,12 +19,12 @@ def max_sum(array: List[int], k: int) -> int: 24 >>> k = 10 >>> max_sum(arr,k) - Invalid input - -1 + Traceback (most recent call last): + ... + ValueError: Invalid Input """ if len(array) < k or k < 0: - print("Invalid input") - return -1 + raise ValueError("Invalid Input") max_sum = -sys.maxsize current_sum = sum(array[:k]) for i in range(len(array) - k): From 1de021d80b46a6e1af6070313fdb0cc6a8bd3015 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 9 Oct 2020 15:35:19 +0530 Subject: [PATCH 4/7] Update max_sum_sliding_window.py --- other/max_sum_sliding_window.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py index 9117e69c19fe..d8e1a0ab040c 100644 --- a/other/max_sum_sliding_window.py +++ b/other/max_sum_sliding_window.py @@ -29,8 +29,8 @@ def max_sum(array: List[int], k: int) -> int: current_sum = sum(array[:k]) for i in range(len(array) - k): current_sum = current_sum - array[i] + array[i + k] - current_sum = max(max_sum, current_sum) - return current_sum + max_sum = max(max_sum, current_sum) + return max_sum if __name__ == "__main__": From 6b2101f99d0d765ef244d392f8a362ce224760e9 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 9 Oct 2020 15:37:52 +0530 Subject: [PATCH 5/7] Added more tests --- other/max_sum_sliding_window.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py index d8e1a0ab040c..97d2be508ffa 100644 --- a/other/max_sum_sliding_window.py +++ b/other/max_sum_sliding_window.py @@ -22,6 +22,10 @@ def max_sum(array: List[int], k: int) -> int: Traceback (most recent call last): ... ValueError: Invalid Input + >>> arr = [1, 4, 2, 10, 2, 13, 1, 0, 2] + >>> k = 4 + >>> max_sum(arr, k) + 27 """ if len(array) < k or k < 0: raise ValueError("Invalid Input") From b5c66d28abbbb51c29eada1470e5eaffa1616a1e Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 9 Oct 2020 16:11:02 +0530 Subject: [PATCH 6/7] Update max_sum_sliding_window.py --- other/max_sum_sliding_window.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py index 97d2be508ffa..05eaecbc1159 100644 --- a/other/max_sum_sliding_window.py +++ b/other/max_sum_sliding_window.py @@ -10,21 +10,21 @@ from typing import List -def max_sum(array: List[int], k: int) -> int: +def 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(arr, k) + >>> max_sum_in_array(arr, k) 24 >>> k = 10 - >>> max_sum(arr,k) + >>> 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(arr, k) + >>> max_sum_in_array(arr, k) 27 """ if len(array) < k or k < 0: @@ -44,4 +44,4 @@ def max_sum(array: List[int], k: int) -> int: testmod() array = [randint(-1000, 1000) for i in range(100)] k = randint(0, 110) - print(f"The maximum sum of {k} consecutive elements is {max_sum(array,k)}") + print(f"The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}") From 3a07ad658bd9813e4fc195cbb80de90b2bd3b50b Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 9 Oct 2020 16:40:34 +0530 Subject: [PATCH 7/7] Update max_sum_sliding_window.py --- other/max_sum_sliding_window.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py index 05eaecbc1159..4be7d786f215 100644 --- a/other/max_sum_sliding_window.py +++ b/other/max_sum_sliding_window.py @@ -6,7 +6,6 @@ called 'Window sliding technique' where the nested loops can be converted to a single loop to reduce time complexity. """ -import sys from typing import List @@ -29,8 +28,7 @@ def max_sum_in_array(array: List[int], k: int) -> int: """ if len(array) < k or k < 0: raise ValueError("Invalid Input") - max_sum = -sys.maxsize - current_sum = sum(array[:k]) + max_sum = current_sum = sum(array[:k]) for i in range(len(array) - k): current_sum = current_sum - array[i] + array[i + k] max_sum = max(max_sum, current_sum)