What is a Subarray?
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 Problem Statement: Subarray with a Given Sum
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.
Example Problem
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
.
Approaches to Solve the Problem
Brute Force Approach
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.
Implementation:
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:
- Time Complexity: O(n^3) - due to the summation being an O(n) operation inside of two nested loops.
- Space Complexity: O(1) - no additional space is used other than variables.
Optimized Approach: Hashing
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.
Implementation:
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:
- Time Complexity: O(n) - single traversal of the array.
- Space Complexity: O(n) - in the worst case, we are storing every cumulative sum.
Example Walkthrough
Let's revisit the previous example with arr = [1, 2, 3, 7, 5]
and target_sum = 12
:
- Start with cumulative_sum = 0 and an empty sum_dict.
- Add
1
: cumulative_sum = 1. (sum_dict = {1: True}) - Add
2
: cumulative_sum = 3. (sum_dict = {1: True, 3: True}) - Add
3
: cumulative_sum = 6. (sum_dict = {1: True, 3: True, 6: True}) - Add
7
: cumulative_sum = 13. (check for13 - 12 = 1
insum_dict
→ exists, return True).
An Important Note on Edge Cases
When implementing solutions for subarray problems, it's crucial to test edge cases, such as:
- An empty array.
- An array where no subarray triggers the target sum.
- Arrays with negative numbers.
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!