From 31d69523885ccf967b424dcdf310e00aaa46cb01 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 28 Jun 2026 05:22:03 -0600 Subject: [PATCH] adding updates --- ...5_maximum_size_of_subarray_sum_equals_k.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/my_project/interviews/google_top_exercises/round_1/05_maximum_size_of_subarray_sum_equals_k.py diff --git a/src/my_project/interviews/google_top_exercises/round_1/05_maximum_size_of_subarray_sum_equals_k.py b/src/my_project/interviews/google_top_exercises/round_1/05_maximum_size_of_subarray_sum_equals_k.py new file mode 100644 index 00000000..1e2d584f --- /dev/null +++ b/src/my_project/interviews/google_top_exercises/round_1/05_maximum_size_of_subarray_sum_equals_k.py @@ -0,0 +1,25 @@ +from typing import List + +class Solution: + def maxSubArrayLen(self, nums: List[int], k: int) -> int: + prefix_sum = longest_subarray = 0 + indices = {} + + for i, num in enumerate(nums): + prefix_sum += num + + # Check if all of the numbers seen so far sum to k. + if prefix_sum == k: + longest_subarray = i + 1 + + # If any subarray seen so far sums to k, then + # update the length of the longest_subarray. + if prefix_sum - k in indices: + longest_subarray = max(longest_subarray, i - indices[prefix_sum - k]) + + # Only add the current prefix_sum index pair to the + # map if the prefix_sum is not already in the map. + if prefix_sum not in indices: + indices[prefix_sum] = i + + return longest_subarray \ No newline at end of file