2395. Find Subarrays With Equal Sum LeetCode Solution
In this guide, you will get 2395. Find Subarrays With Equal Sum LeetCode Solution with the best time and space complexity. The solution to Find Subarrays With Equal Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Subarrays With Equal Sum solution in C++
- Find Subarrays With Equal Sum solution in Java
- Find Subarrays With Equal Sum solution in Python
- Additional Resources

Problem Statement of Find Subarrays With Equal Sum
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2395. Find Subarrays With Equal Sum LeetCode Solution in C++
class Solution {
public:
bool findSubarrays(vector<int>& nums) {
unordered_set<int> seen;
for (int i = 1; i < nums.size(); ++i)
if (!seen.insert(nums[i - 1] + nums[i]).second)
return true;
return false;
}
};
/* code provided by PROGIEZ */
2395. Find Subarrays With Equal Sum LeetCode Solution in Java
class Solution {
public boolean findSubarrays(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int i = 1; i < nums.length; ++i) {
final int sum = nums[i - 1] + nums[i];
if (seen.contains(sum))
return true;
seen.add(sum);
}
return false;
}
}
// code provided by PROGIEZ
2395. Find Subarrays With Equal Sum LeetCode Solution in Python
class Solution:
def findSubarrays(self, nums: list[int]) -> bool:
seen = set()
for a, b in zip(nums, nums[1:]):
summ = a + b
if summ in seen:
return True
seen.add(summ)
return False
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.