From b6d048c6abcbb3a1fc41ae7a8bea0693ed968cc9 Mon Sep 17 00:00:00 2001 From: VIRAJ <124988569+viraj200524@users.noreply.github.com> Date: Sun, 13 Oct 2024 02:13:16 +0530 Subject: [PATCH 1/2] ADDED SLIDING WINDOW ALGORITHM TO CALCULATE MAX SUM OF SUBARRAY OF SIZE K OF A ARRAY --- data_structures/arrays/sliding_window.py | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 data_structures/arrays/sliding_window.py diff --git a/data_structures/arrays/sliding_window.py b/data_structures/arrays/sliding_window.py new file mode 100644 index 000000000000..ce3d031906cc --- /dev/null +++ b/data_structures/arrays/sliding_window.py @@ -0,0 +1,43 @@ +def max_sum_subarray(arr, k): + # Edge case: if the window size is greater than the array length + if len(arr) < k: + return None + + # Compute the sum of the first window + window_sum = sum(arr[:k]) + max_sum = window_sum + + # Slide the window from left to right + for i in range(k, len(arr)): + # Subtract the element going out of the window and add the new element coming into the window + window_sum += arr[i] - arr[i - k] + max_sum = max(max_sum, window_sum) + + return max_sum + +# Example usage: + +# Example 1: Larger array +arr1 = [4, 3, 10, 2, 8, 6, 7, 1, 9] +k1 = 4 +print("Example 1: Maximum sum of subarray of length", k1, "is", max_sum_subarray(arr1, k1)) + +# Example 2: All elements are negative +arr2 = [-2, -3, -1, -5, -6] +k2 = 2 +print("Example 2: Maximum sum of subarray of length", k2, "is", max_sum_subarray(arr2, k2)) + +# Example 3: Array with all elements equal +arr3 = [5, 5, 5, 5, 5, 5] +k3 = 3 +print("Example 3: Maximum sum of subarray of length", k3, "is", max_sum_subarray(arr3, k3)) + +# Example 4: Small array +arr4 = [1, 2] +k4 = 2 +print("Example 4: Maximum sum of subarray of length", k4, "is", max_sum_subarray(arr4, k4)) + +# Example 5: k greater than the array length +arr5 = [7, 8, 9] +k5 = 5 +print("Example 5: Maximum sum of subarray of length", k5, "is", max_sum_subarray(arr5, k5)) From cd92e2648cb27346e5f6b6d0b54c092c6bb0c58a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Oct 2024 20:51:07 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/sliding_window.py | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/data_structures/arrays/sliding_window.py b/data_structures/arrays/sliding_window.py index ce3d031906cc..d30f2cf6eb2e 100644 --- a/data_structures/arrays/sliding_window.py +++ b/data_structures/arrays/sliding_window.py @@ -2,42 +2,53 @@ def max_sum_subarray(arr, k): # Edge case: if the window size is greater than the array length if len(arr) < k: return None - + # Compute the sum of the first window window_sum = sum(arr[:k]) max_sum = window_sum - + # Slide the window from left to right for i in range(k, len(arr)): # Subtract the element going out of the window and add the new element coming into the window window_sum += arr[i] - arr[i - k] max_sum = max(max_sum, window_sum) - + return max_sum + # Example usage: # Example 1: Larger array arr1 = [4, 3, 10, 2, 8, 6, 7, 1, 9] k1 = 4 -print("Example 1: Maximum sum of subarray of length", k1, "is", max_sum_subarray(arr1, k1)) +print( + "Example 1: Maximum sum of subarray of length", k1, "is", max_sum_subarray(arr1, k1) +) # Example 2: All elements are negative arr2 = [-2, -3, -1, -5, -6] k2 = 2 -print("Example 2: Maximum sum of subarray of length", k2, "is", max_sum_subarray(arr2, k2)) +print( + "Example 2: Maximum sum of subarray of length", k2, "is", max_sum_subarray(arr2, k2) +) # Example 3: Array with all elements equal arr3 = [5, 5, 5, 5, 5, 5] k3 = 3 -print("Example 3: Maximum sum of subarray of length", k3, "is", max_sum_subarray(arr3, k3)) +print( + "Example 3: Maximum sum of subarray of length", k3, "is", max_sum_subarray(arr3, k3) +) # Example 4: Small array arr4 = [1, 2] k4 = 2 -print("Example 4: Maximum sum of subarray of length", k4, "is", max_sum_subarray(arr4, k4)) +print( + "Example 4: Maximum sum of subarray of length", k4, "is", max_sum_subarray(arr4, k4) +) # Example 5: k greater than the array length arr5 = [7, 8, 9] k5 = 5 -print("Example 5: Maximum sum of subarray of length", k5, "is", max_sum_subarray(arr5, k5)) +print( + "Example 5: Maximum sum of subarray of length", k5, "is", max_sum_subarray(arr5, k5) +)