Given a 0-indexed integer array nums
, determine whether there exist two subarrays of length 2
with equal sum. Note that the two subarrays must begin at different indices.
Return true
if these subarrays exist, and false
otherwise.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [4,2,4] Output: true Explanation: The subarrays with elements [4,2] and [2,4] have the same sum of 6.
Example 2:
Input: nums = [1,2,3,4,5] Output: false Explanation: No two subarrays of size 2 have the same sum.
Example 3:
Input: nums = [0,0,0] Output: true Explanation: The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0. Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.
Constraints:
2 <= nums.length <= 1000
-109 <= nums[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking every possible pair of groups within the numbers we're given. We essentially try out every combination to see if any two groups have the same total. Think of it like trying out all possible teams and comparing their scores.
Here's how the algorithm would work step-by-step:
def find_subarrays_with_equal_sum(number_list):
list_length = len(number_list)
for first_subarray_start in range(list_length):
for first_subarray_end in range(first_subarray_start, list_length):
first_subarray_sum = 0
for index in range(first_subarray_start, first_subarray_end + 1):
first_subarray_sum += number_list[index]
# Start iterating for the second subarray.
for second_subarray_start in range(list_length):
for second_subarray_end in range(second_subarray_start, list_length):
# Avoid overlapping subarrays.
if first_subarray_end < second_subarray_start or second_subarray_end < first_subarray_start:
second_subarray_sum = 0
for index in range(second_subarray_start, second_subarray_end + 1):
second_subarray_sum += number_list[index]
# Found a match!
if first_subarray_sum == second_subarray_sum:
return True
# No matching subarrays found.
return False
The fastest way to find if two equal-sum subarrays exist is to check for duplicate sums. If we encounter the same subarray sum twice while going through the numbers, it means we found two subarrays that add up to the same total. This avoids unnecessary checks.
Here's how the algorithm would work step-by-step:
def find_subarrays_with_equal_sum(numbers) -> bool:
seen_sums = set()
current_sum = 0
for number in numbers:
current_sum += number
# If the current sum has already been seen,
# it indicates a duplicate subarray sum.
if current_sum in seen_sums:
return True
# Store the current sum to check for duplicates later.
seen_sums.add(current_sum)
# If no duplicate subarray sums are found,
# then no two subarrays have equal sums.
return False
Case | How to Handle |
---|---|
Null or empty input array | Return false immediately, as no subarrays of length 2 can exist. |
Input array with only one element | Return false because a subarray of length 2 cannot be formed. |
Input array with exactly two elements and they are equal | Calculate sum once, compare only with itself which should return false as indices must differ. |
Array with a large number of elements containing duplicates | The hash set approach ensures efficient checking regardless of duplicate presence, scaling linearly with array size. |
Array containing only negative numbers | The algorithm handles negative numbers correctly as the sum comparison will work regardless of sign. |
Array containing zero(s) | The algorithm handles zeros correctly as the sum comparison will work regardless of value. |
Array with integer overflow potential when summing two elements | Use a long data type to store the sum to prevent integer overflow, ensuring accurate comparison. |
No two subarrays of length two have the same sum | The algorithm correctly returns false after iterating through the entire array and finding no matching sums in different positions. |