Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
navidre authored Feb 5, 2024
1 parent dfb7f36 commit d942470
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,31 @@ Let's look at an example where you need to find the maximum sum of any consecuti

```python
def max_sum_subarray(arr, k):
# Initialize max_sum to 0. This will store the maximum sum found.
max_sum = 0

# Calculate the sum of the first 'k' elements in the array.
# This is our initial window sum.
window_sum = sum(arr[:k])

# Loop through the array, but only until len(arr) - k.
# This is because we are considering 'k' elements at a time,
# and we stop when we reach the last 'k' elements.
for i in range(len(arr) - k):
# Slide the window forward by one element:
# Subtract the element going out of the window (arr[i])
# and add the new element entering into the window (arr[i + k]).
window_sum = window_sum - arr[i] + arr[i + k]

# Update max_sum if the current window_sum is greater than the previously recorded max_sum.
max_sum = max(max_sum, window_sum)

# Return the maximum sum found.
return max_sum

# Example usage
arr = [1, 4, 2, 10, 23, 3, 1, 0, 20]
k = 4
arr = [1, 4, 2, 10, 23, 3, 1, 0, 20] # Input array
k = 4 # Number of consecutive elements to consider
print(max_sum_subarray(arr, k)) # Output will be the maximum sum of 4 consecutive elements
```

Expand All @@ -40,24 +53,37 @@ Now, let's look at a variable-size window problem, like finding the smallest sub

```python
def smallest_subarray_with_given_sum(arr, s):
# Initialize min_length with infinity. This variable will hold the length of the smallest subarray.
min_length = float('inf')

# Initialize window_sum to 0. It will store the sum of elements in the current window.
window_sum = 0

# Initialize window_start to 0. It marks the start of the sliding window.
window_start = 0

# Iterate over the array using window_end as the end of the sliding window.
for window_end in range(len(arr)):
# Add the current element to the window_sum.
window_sum += arr[window_end]

# Shrink the window from the start if the window_sum is greater than or equal to s.
while window_sum >= s:
# Update min_length with the smaller length between the previous min_length and current window size.
min_length = min(min_length, window_end - window_start + 1)

# Subtract the element at window_start from window_sum and move window_start forward.
window_sum -= arr[window_start]
window_start += 1

# Return min_length if a subarray was found; otherwise, return 0.
# Checking against float('inf') is necessary to handle the case where no such subarray is found.
return min_length if min_length != float('inf') else 0

# Example usage
arr = [2, 1, 5, 2, 3, 2]
s = 7
print(smallest_subarray_with_given_sum(arr, s)) # Output will be the length of the smallest subarray
arr = [2, 1, 5, 2, 3, 2] # Input array
s = 7 # Target sum
print(smallest_subarray_with_given_sum(arr, s)) # Output will be the length of the smallest subarray with sum >= s
```

### Real-World Application:
Expand Down

0 comments on commit d942470

Please sign in to comment.