Find Subarrays With Equal Sum LeetCode Solution

Problem – Find Subarrays With Equal Sum LeetCode Solution

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.

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

Find Subarrays With Equal Sum LeetCode Solution in C++

class Solution {
public:
	bool findSubarrays(vector<int>& nums) {
		int n=nums.size();
		int count=0;
		for(int i=0;i<n-1;i++){
			for(int j=i+1;j<n-1;j++){
				if(nums[j]+nums[j+1]==nums[i]+nums[i+1]) count++;
			}
		}
		return count>=1;
	}
};

Find Subarrays With Equal Sum LeetCode Solution in Python

class Solution:
    def findSubarrays(self, nums: List[int]) -> bool:
        sums = set()
        
        for i in range(len(nums)-1):
            t = nums[i]+nums[i+1]
            if t in sums:
                return True
            else:
                sums.add(t)
            
        return False

Find Subarrays With Equal Sum LeetCode Solution in Java

class Solution {
    public boolean findSubarrays(int[] nums) {
        
        if(nums.length<2)
        return false;
        
        HashSet<Integer> set=new HashSet<>(); //  To Track previous sum calculated 
        
        for(int i=0;i<nums.length-1;i++){
            
            if(!set.add(nums[i]+nums[i+1]))  //   Check if sum already exist 
            return true;
            
        }
        
        return false;    
        
    }
}

Find Subarrays With Equal Sum LeetCode Solution Review:

In our experience, we suggest you solve this Find Subarrays With Equal Sum LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Find Subarrays With Equal Sum LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Find Subarrays With Equal Sum LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *