{"id":9796,"date":"2022-09-07T16:31:54","date_gmt":"2022-09-07T23:31:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9796"},"modified":"2022-09-07T16:33:10","modified_gmt":"2022-09-07T23:33:10","slug":"leetcode-2395-find-subarrays-with-equal-sum%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2395-find-subarrays-with-equal-sum%ef%bf%bc\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2395.\u00a0Find Subarrays With Equal Sum"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, determine whether there exist&nbsp;<strong>two<\/strong>&nbsp;subarrays of length&nbsp;<code>2<\/code>&nbsp;with&nbsp;<strong>equal<\/strong>&nbsp;sum. Note that the two subarrays must begin at&nbsp;<strong>different<\/strong>&nbsp;indices.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if these subarrays exist, and&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/p>\n\n\n\n<p>A&nbsp;<strong>subarray<\/strong>&nbsp;is a contiguous non-empty sequence of elements within an array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [4,2,4]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The subarrays with elements [4,2] and [2,4] have the same sum of 6.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> No two subarrays of size 2 have the same sum.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,0,0]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0. \nNote 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.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashset<\/strong><\/h2>\n\n\n\n<p>Use a hashset to track all the sums seen so far.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool findSubarrays(vector<int>& nums) {\n    unordered_set<int> s;\n    for (int i = 1; i < nums.size(); ++i)\n      if (!s.emplace(nums[i] + nums[i - 1]).second) return true;    \n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, determine whether there exist&nbsp;two&nbsp;subarrays of length&nbsp;2&nbsp;with&nbsp;equal&nbsp;sum. Note that the two subarrays must begin at&nbsp;different&nbsp;indices. Return&nbsp;true&nbsp;if these subarrays exist, and&nbsp;false&nbsp;otherwise. A&nbsp;subarray&nbsp;is a contiguous&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,62],"class_list":["post-9796","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9796"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9796\/revisions"}],"predecessor-version":[{"id":9799,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9796\/revisions\/9799"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}