Created
July 18, 2023 04:56
-
-
Save Aisuko/9551c74b3fedd83ec6ba1e0c2a022e3c to your computer and use it in GitHub Desktop.
Revisions
-
Aisuko created this gist
Jul 18, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ from __future__ import annotations def max_sum(arr, k): """ It is used to find the maxumum or minimum sum, product, etc. Time complexity: O(n) """ n = len(arr) if n < k: print("Invalid operation") return -1 window_sum = sum(arr[:k]) max_sum = window_sum for i in range(n-k): window_sum = window_sum - arr[i] + arr[i+k] max_sum = max(window_sum, max_sum) return max_sum if __name__ == "__main__": assert max_sum([100,200,300,400], 2) == 700 assert max_sum([2,3], 4) == -1