Find Subarrays With Equal Sum
The problem asks for determining whether there are two subarrays of length 2 with equal sum. To find the solution, we can iterate through the array, calculating the sum of every 2 elements, and keep track of the sums we’ve already seen. If we find a sum that we’ve already seen before, we return true
. If we go through the entire array without finding two subarrays with the same sum, we return false
.
Here’s a method to implement this logic:
|
|
Example
For the input nums = [4,2,4]
, the method would return true
, since the subarrays [4,2]
and [2,4]
have the same sum of 6
.