|
| 1 | +# [2395. Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum) |
| 2 | + |
| 3 | +[中文文档](/solution/2300-2399/2395.Find%20Subarrays%20With%20Equal%20Sum/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, determine whether there exist <strong>two</strong> subarrays of length <code>2</code> with <strong>equal</strong> sum. Note that the two subarrays must begin at <strong>different</strong> indices.</p> |
| 8 | + |
| 9 | +<p>Return <code>true</code><em> if these subarrays exist, and </em><code>false</code><em> otherwise.</em></p> |
| 10 | + |
| 11 | +<p>A <b>subarray</b> is a contiguous non-empty sequence of elements within an array.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong>Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> nums = [4,2,4] |
| 18 | +<strong>Output:</strong> true |
| 19 | +<strong>Explanation:</strong> The subarrays with elements [4,2] and [2,4] have the same sum of 6. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong>Example 2:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> nums = [1,2,3,4,5] |
| 26 | +<strong>Output:</strong> false |
| 27 | +<strong>Explanation:</strong> No two subarrays of size 2 have the same sum. |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong>Example 3:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> nums = [0,0,0] |
| 34 | +<strong>Output:</strong> true |
| 35 | +<strong>Explanation:</strong> The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0. |
| 36 | +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. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>2 <= nums.length <= 1000</code></li> |
| 44 | + <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> |
| 45 | +</ul> |
| 46 | + |
| 47 | + |
| 48 | +## Solutions |
| 49 | + |
| 50 | +<!-- tabs:start --> |
| 51 | + |
| 52 | +### **Python3** |
| 53 | + |
| 54 | +```python |
| 55 | +class Solution: |
| 56 | + def findSubarrays(self, nums: List[int]) -> bool: |
| 57 | + s = set() |
| 58 | + for a, b in pairwise(nums): |
| 59 | + if (v := a + b) in s: |
| 60 | + return True |
| 61 | + s.add(v) |
| 62 | + return False |
| 63 | +``` |
| 64 | + |
| 65 | +### **Java** |
| 66 | + |
| 67 | +```java |
| 68 | +class Solution { |
| 69 | + public boolean findSubarrays(int[] nums) { |
| 70 | + Set<Integer> s = new HashSet<>(); |
| 71 | + for (int i = 0; i < nums.length - 1; ++i) { |
| 72 | + int v = nums[i] + nums[i + 1]; |
| 73 | + if (!s.add(v)) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + return false; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### **C++** |
| 83 | + |
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + bool findSubarrays(vector<int>& nums) { |
| 88 | + unordered_set<int> s; |
| 89 | + for (int i = 0; i < nums.size() - 1; ++i) { |
| 90 | + int v = nums[i] + nums[i + 1]; |
| 91 | + if (s.count(v)) return true; |
| 92 | + s.insert(v); |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
| 98 | +
|
| 99 | +### **Go** |
| 100 | +
|
| 101 | +```go |
| 102 | +func findSubarrays(nums []int) bool { |
| 103 | + s := map[int]bool{} |
| 104 | + for i := 0; i < len(nums)-1; i++ { |
| 105 | + v := nums[i] + nums[i+1] |
| 106 | + if s[v] { |
| 107 | + return true |
| 108 | + } |
| 109 | + s[v] = true |
| 110 | + } |
| 111 | + return false |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### **TypeScript** |
| 116 | + |
| 117 | +```ts |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +### **...** |
| 122 | + |
| 123 | +``` |
| 124 | +
|
| 125 | +
|
| 126 | +``` |
| 127 | + |
| 128 | +<!-- tabs:end --> |
0 commit comments