Menu Close

Leetcode 2395: Find Subarrays With Equal Sum Solution

Here, we will help to understand about how to solve Find Subarrays With Equal Sum Solution of leet code 2395 with code and algorithm.

You are given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Here, 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 = [2,3,4,6,1,4,10]
Output: true
Explanation: The subarrays with elements [2,3] and [1,4] have the same sum of 5.

2)
Input: nums = [4,2,4]
Output: true
Explanation: The subarrays with elements [4,2] and [2,4] have the same sum of 6.

3)
Input: nums = [1,2,3,4,5]
Output: false
Explanation: No two subarrays of size 2 have the same sum.

4)
Input: nums = [1,2,8,9,4,1,9,11]
Output: true
Explanation: The subarrays with elements [2,8] and [1,9] have the same sum of 10.

5)
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.

Find Subarrays With Equal Sum in C++

Code 1:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

bool findSubarrays(vector<int>& nums) {
    set<int> s;
        
    for(int i = 0; i < nums.size() - 1; i++) {
        if(s.find(nums[i] + nums[i+1]) != s.end()) {
            return true;
        }
        s.insert(nums[i] + nums[i+1]);
    }
    return false;
}
    
int main()
{
    vector<int> nums = {2,3,4,6,1,4,10};
    
    cout<<findSubarrays(nums);

    return 0;
}

Code 2:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

bool findSubarrays(vector<int>& nums) {
    map<int, bool> m;
        
    for(int i = 0; i < nums.size() - 1; i++) {
        if(m.find(nums[i] + nums[i+1]) != m.end()) {
            return true;
        }
        m[nums[i] + nums[i+1]] = true;
    }
    return false;
}
    
int main()
{
    vector<int> nums = {2,3,4,6,1,4,10};
    
    cout<<findSubarrays(nums);

    return 0;
}

Code 3:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

bool findSubarrays(vector<int>& nums) {
    for(int i = 0; i < nums.size() - 1; i++) {
        for(int j = i+1; j < nums.size() - 1; j++) {
            if(nums[i] + nums[i+1] == nums[j] + nums[j+1]) {
                return true;
            }
        }
    }
    return false;
}
    
int main()
{
    vector<int> nums = {2,3,4,6,1,4,10};
    
    cout<<findSubarrays(nums);

    return 0;
}

Output:

true

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Posted in C++, Easy, Leetcode

Leave a Reply

Your email address will not be published.

Contact Us