Skip to content

ADDED SLIDING WINDOW ALGORITHM TO CALCULATE MAX SUM OF SUBARRAY OF SI… #12021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions data_structures/arrays/sliding_window.py
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 5 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/sliding_window.py:5:1: W293 Blank line contains whitespace

Check failure on line 5 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/sliding_window.py:5:1: W293 Blank line contains whitespace
# Compute the sum of the first window
window_sum = sum(arr[:k])
max_sum = window_sum

Check failure on line 9 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/sliding_window.py:9:1: W293 Blank line contains whitespace

Check failure on line 9 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/sliding_window.py:9:1: W293 Blank line contains whitespace
# 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

Check failure on line 12 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:12:89: E501 Line too long (101 > 88)

Check failure on line 12 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:12:89: E501 Line too long (101 > 88)
window_sum += arr[i] - arr[i - k]
max_sum = max(max_sum, window_sum)

Check failure on line 15 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/sliding_window.py:15:1: W293 Blank line contains whitespace

Check failure on line 15 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/sliding_window.py:15:1: W293 Blank line contains whitespace
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))

Check failure on line 23 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:23:89: E501 Line too long (91 > 88)

Check failure on line 23 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:23:89: E501 Line too long (91 > 88)

# 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))

Check failure on line 28 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:28:89: E501 Line too long (91 > 88)

Check failure on line 28 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:28:89: E501 Line too long (91 > 88)

# 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))

Check failure on line 33 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:33:89: E501 Line too long (91 > 88)

Check failure on line 33 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:33:89: E501 Line too long (91 > 88)

# 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))

Check failure on line 38 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:38:89: E501 Line too long (91 > 88)

Check failure on line 38 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:38:89: E501 Line too long (91 > 88)

# 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))

Check failure on line 43 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:43:89: E501 Line too long (91 > 88)

Check failure on line 43 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:43:89: E501 Line too long (91 > 88)
Loading