To start, let's clarify what a subarray is. A subarray is a contiguous segment of an array. For instance, given the array [1, 2, 3, 4]
, the possible subarrays are:
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[2]
[2, 3]
[2, 3, 4]
[3]
[3, 4]
[4]
In contrast to subsets, which can be any combination of elements regardless of their order or contiguity, subarrays maintain the original ordering and continuity of elements.
The subarray with a given sum problem asks us to determine if there exists a contiguous subarray within a given array that sums to a specified target value. This is a frequent problem encountered in coding interviews and competitive programming.
Consider the array arr = [1, 2, 3, 7, 5]
and the target sum 12
. We need to identify if there exists a contiguous subarray whose elements sum up to 12
.
In this case, the subarray [2, 3, 7]
sums to 12
, so the answer is True
.
One straightforward method is to use a brute force approach, where we examine all possible contiguous subarrays and check their sums. This involves two nested loops.
def subarray_with_sum_brute_force(arr, target_sum): n = len(arr) for i in range(n): for j in range(i, n): if sum(arr[i:j+1]) == target_sum: return True return False
Complexity:
A much more efficient approach involves using a hash map (or dictionary) to track the cumulative sums of the elements. The idea here is to maintain a running sum as we traverse the array and check if the difference between the current sum and the target sum has been seen before.
def subarray_with_sum_hashing(arr, target_sum): cumulative_sum = 0 sum_dict = {} for num in arr: cumulative_sum += num if cumulative_sum == target_sum: return True if (cumulative_sum - target_sum) in sum_dict: return True sum_dict[cumulative_sum] = True return False
Complexity:
Let's revisit the previous example with arr = [1, 2, 3, 7, 5]
and target_sum = 12
:
1
: cumulative_sum = 1. (sum_dict = {1: True})2
: cumulative_sum = 3. (sum_dict = {1: True, 3: True})3
: cumulative_sum = 6. (sum_dict = {1: True, 3: True, 6: True})7
: cumulative_sum = 13. (check for 13 - 12 = 1
in sum_dict
→ exists, return True).When implementing solutions for subarray problems, it's crucial to test edge cases, such as:
These cases can affect the logic, especially when using cumulative sums.
By understanding both the naive and optimized approaches, you can choose the right strategy for your specific needs, be it for competitions or in the professional realm. Happy coding!
13/10/2024 | DSA
06/12/2024 | DSA
16/11/2024 | DSA
15/11/2024 | DSA
23/09/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
23/09/2024 | DSA
23/09/2024 | DSA
16/11/2024 | DSA
16/11/2024 | DSA