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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, k, n
    integer, parameter :: nmax = 1000
    integer :: nums(nmax)
    logical :: result

    ! Example 1
    nums = (/4, 2, 4/)
    write(*,*) 'Example 1'
    write(*,*) 'Input: ', nums
    result = equal_sum_subarrays(nums)
    write(*,*) 'Output: ', result
    write(*,*) ''

    ! Example 2
    nums = (/1, 2, 3, 4, 5/)
    write(*,*) 'Example 2'
    write(*,*) 'Input: ', nums
    result = equal_sum_subarrays(nums)
    write(*,*) 'Output: ', result
    write(*,*) ''

    ! Example 3
    nums = (/0, 0, 0/)
    write(*,*) 'Example 3'
    write(*,*) 'Input: ', nums
    result = equal_sum_subarrays(nums)
    write(*,*) 'Output: ', result
    write(*,*) ''

contains

    function equal_sum_subarrays(nums) result(result)
        implicit none
        integer, intent(in) :: nums(:)
        logical :: result
        integer :: i, j, k, n

        n = size(nums)
        do i = 1, n - 1
            do j = i + 1, n
                if (sum(nums(i:j)) == sum(nums(j:i))) then
                    result = .true.
                    return
                end if
            end do
        end do
        result = .false.
    end function equal_sum_subarrays

end program main
Compiled
Executed
Correct
! Declare variables
integer :: i, j, k, l, m, n, sum1, sum2, equal_sum
logical :: found_equal_sum

! Initialize variables
i = 1
j = 2
k = 3
l = 4
m = 5
n = 6
sum1 = 0
sum2 = 0
equal_sum = 0
found_equal_sum = .false.

! Loop through the array
do while (i < n)
    ! Calculate the sum of the first subarray
    sum1 = sum(nums(i:j))

    ! Calculate the sum of the second subarray
    sum2 = sum(nums(k:l))

    ! Check if the sums are equal
    if (sum1 == sum2) then
        ! If the sums are equal, set the flag to true
        found_equal_sum = .true.

        ! Exit the loop
        exit
    end if

    ! Increment the indices
    i = i + 1
    j = j + 1
    k = k + 1
    l = l + 1
end do

! Output the result
if (found_equal_sum) then
    write (*,*) "True"
else
    write (*,*) "False"
end if

end
🌐 Data from online sources
def equal_sum_subarrays(nums):
    sums = set()
    for i in range(1, len(nums)):
        current_sum = nums[i] + nums[i - 1]
        if current_sum in sums:
            return True
        sums.add(current_sum)
    return False

The algorithm works by iterating through the array, calculating the sum of each pair of adjacent elements, and using a set to store the seen sums. If the current sum already exists in the set, it means that there are two subarrays with equal sums, so the function returns true. At the end of the loop, if no such subarrays were found, the function returns false.

Time complexity is O(n), where n is the size of the input array, since we are iterating through the elements only once. Space complexity is also O(n) as at worst we have to store all possible sums in the set.

🌐 Data from online sources
bool equalSumSubarrays(vector<int>& nums) {
    unordered_set<int> sums;
    for (int i = 1; i < nums.size(); ++i) {
        int sum = nums[i] + nums[i - 1];
        if (sums.count(sum)) return true;
        sums.insert(sum);
    }
    return false;
}

The algorithm works by iterating through the array, calculating the sum of each pair of adjacent elements, and using a set to store the seen sums. If the current sum already exists in the set, it means that there are two subarrays with equal sums, so the function returns true. At the end of the loop, if no such subarrays were found, the function returns false.

Time complexity is O(n), where n is the size of the input array, since we are iterating through the elements only once. Space complexity is also O(n) as at worst we have to store all possible sums in the set.