Open carloscn opened 1 year ago
static int32_t find_subarrays(int32_t *nums, size_t nums_size)
{
int32_t ret = 0;
int32_t *sum_rom = NULL;
UTILS_CHECK_PTR(nums);
UTILS_CHECK_LEN(nums_size);
sum_rom = (int32_t *)calloc(1 << nums_size, sizeof(int32_t));
UTILS_CHECK_PTR(sum_rom);
size_t n = 0;
for (size_t i = 0; i < nums_size - 1; i ++) {
for (size_t j = i + 1; j < nums_size; j ++) {
int32_t sum = nums[i] + nums[j];
for (size_t k = 0; k < n; k ++) {
if (sum == sum_rom[k]) {
ret ++;
goto finish;
}
}
sum_rom[n ++] = sum;
}
}
finish:
UTILS_SAFE_FREE(sum_rom);
return ret;
}
Description
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