id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n=nums.size();\n vector<pair<ll,ll>>temp;\n for(int i=0;i<n;i++)\n {\n temp.push_back({nums[i],i});\n }\n sort(temp.begin(),temp.end());\n vector<ll>ps(n),ss(n),pd(n),sd(n);\n ps[0]=cost[temp[0].second];pd[0]=0;\n ss[n-1]=cost[temp[n-1].second]; sd[n-1]=0;\n for(int i=1;i<n;i++)\n {\n ps[i]=ps[i-1]+cost[temp[i].second];\n pd[i]=pd[i-1]+(ps[i-1]*(temp[i].first-temp[i-1].first));\n }\n cout<<sd[0]<<endl;\n for(int i=n-2;i>=0;i--)\n {\n ss[i]=ss[i+1]+cost[temp[i].second];\n sd[i]=sd[i+1]+ss[i+1]*(abs(temp[i].first-temp[i+1].first));\n }\n ll ans=min(pd[n-1],sd[0]);\n for(int i=1;i<=n-2;i++)\n {\n ans=min(ans,pd[i]+sd[i]);\n }\n return ans;\n }\n};",
"memory": "54200"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n = nums.size();\n vector<long long> prefixCost(n);\n vector<long long> prefixMul(n);\n vector<long long> suffixCost(n);\n vector<long long> suffixMul(n);\n vector<pair<long long, long long>> numCost;\n for(int i = 0; i < nums.size(); i++) {\n numCost.push_back(make_pair(nums[i], cost[i]));\n }\n sort(numCost.begin(), numCost.end());\n for(int i = 0; i < n; i++) {\n if(i == 0) {\n prefixCost[i] = numCost[i].second;\n prefixMul[i] = numCost[i].second * numCost[i].first;\n } else {\n prefixCost[i] = prefixCost[i - 1] + numCost[i].second;\n prefixMul[i] = prefixMul[i - 1] + numCost[i].second * numCost[i].first;\n }\n }\n for(int i = n - 1; i >= 0; i--) {\n if(i == n - 1) {\n suffixCost[i] = numCost[i].second;\n suffixMul[i] = numCost[i].second * numCost[i].first;\n } else {\n suffixCost[i] = suffixCost[i + 1] + numCost[i].second;\n suffixMul[i] = suffixMul[i + 1] + numCost[i].second * numCost[i].first;\n }\n }\n long long ans = LLONG_MAX;\n for(int i = 0; i < n; i++) {\n long long tmp = 0;\n if(i > 0) {\n tmp += numCost[i].first * prefixCost[i - 1];\n tmp -= prefixMul[i - 1];\n }\n if (i < n - 1) {\n tmp -= numCost[i].first * suffixCost[i + 1];\n tmp += suffixMul[i + 1];\n }\n ans = min(tmp, ans);\n }\n return ans;\n }\n};",
"memory": "54300"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n /*\n Observations:\n Even if the values are the same, then the cost may be different\n The answer lies in the values itself\n \n Let us sort both the arrays as per the index\n \n v1, v2, v3, ..x.., v4, v5\n Prefix: (x - v1)c1 + (x - v2)c2 + (x - v3)c3\n : x(c1 + c2 + c3) - (v1c1 + v2c2 + v3c3)\n \n Suffix: (v4 - x)c4 + (v5 - x)c5\n : (v4c4 + v5c5) - x(c4 + c5)\n */\n int n = nums.size();\n vector<array<long long, 2>> numsIdx;\n \n for(int it = 0; it < n; it++)\n {\n numsIdx.push_back({nums[it], it});\n }\n \n sort(begin(numsIdx), end(numsIdx));\n vector<long long> nums1;\n for(auto &x:numsIdx) nums1.emplace_back(x[0]);\n \n vector<long long> costPref(n, 0), constTerm(n, 0);\n \n costPref[0] = cost[numsIdx[0][1]];\n constTerm[0] = numsIdx[0][0] * cost[numsIdx[0][1]];\n \n for(int it = 1; it < n; it++)\n {\n costPref[it] = costPref[it - 1] + cost[numsIdx[it][1]];\n constTerm[it] = constTerm[it - 1] + numsIdx[it][0] * cost[numsIdx[it][1]];\n }\n long long ans = (long long)1e18;\n for(long long x = 1; x <= (int)1e6; x++)\n {\n auto it = lower_bound(nums1.begin(),nums1.end(),x);\n auto index=it-nums1.begin();\n long long costHere = 0;\n if(index)\n costHere+=costPref[index-1]*x-constTerm[index-1];\n if(index<(int)nums.size())\n {\n long long temp=-x*(costPref.back()-(index ? costPref[index-1] : 0))+constTerm.back()-(index ? constTerm[index-1] : 0);\n costHere+=temp;\n }\n ans=min(ans,costHere);\n }\n return ans;\n }\n};",
"memory": "54900"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n /*\n Observations:\n Even if the values are the same, then the cost may be different\n The answer lies in the values itself\n \n Let us sort both the arrays as per the index\n \n v1, v2, v3, ..x.., v4, v5\n Prefix: (x - v1)c1 + (x - v2)c2 + (x - v3)c3\n : x(c1 + c2 + c3) - (v1c1 + v2c2 + v3c3)\n \n Suffix: (v4 - x)c4 + (v5 - x)c5\n : (v4c4 + v5c5) - x(c4 + c5)\n */\n int n = nums.size();\n vector<array<long long, 2>> numsIdx;\n \n for(int it = 0; it < n; it++)\n {\n numsIdx.push_back({nums[it], it});\n }\n \n sort(begin(numsIdx), end(numsIdx));\n vector<long long> nums1;\n for(auto &x:numsIdx) nums1.emplace_back(x[0]);\n \n vector<long long> costPref(n, 0), constTerm(n, 0);\n \n costPref[0] = cost[numsIdx[0][1]];\n constTerm[0] = numsIdx[0][0] * cost[numsIdx[0][1]];\n \n for(int it = 1; it < n; it++)\n {\n costPref[it] = costPref[it - 1] + cost[numsIdx[it][1]];\n constTerm[it] = constTerm[it - 1] + numsIdx[it][0] * cost[numsIdx[it][1]];\n }\n long long ans = (long long)1e18;\n for(long long x: nums)\n {\n auto it = lower_bound(nums1.begin(),nums1.end(),x);\n auto index=it-nums1.begin();\n long long costHere = 0;\n if(index)\n costHere+=costPref[index-1]*x-constTerm[index-1];\n if(index<(int)nums.size())\n {\n long long temp=-x*(costPref.back()-(index ? costPref[index-1] : 0))+constTerm.back()-(index ? constTerm[index-1] : 0);\n costHere+=temp;\n }\n ans=min(ans,costHere);\n }\n return ans;\n }\n};",
"memory": "54900"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n /*\n Observations:\n Even if the values are the same, then the cost may be different\n The answer lies in the values itself\n \n Let us sort both the arrays as per the index\n \n v1, v2, v3, ..x.., v4, v5\n Prefix: (x - v1)c1 + (x - v2)c2 + (x - v3)c3\n : x(c1 + c2 + c3) - (v1c1 + v2c2 + v3c3)\n \n Suffix: (v4 - x)c4 + (v5 - x)c5\n : (v4c4 + v5c5) - x(c4 + c5)\n */\n int n = nums.size();\n vector<array<long long, 2>> numsIdx;\n \n for(int it = 0; it < n; it++)\n {\n numsIdx.push_back({nums[it], it});\n }\n \n sort(begin(numsIdx), end(numsIdx));\n vector<long long> nums1;\n \n for(auto &x:numsIdx) nums1.emplace_back(x[0]);\n \n vector<long long> costPref(n, 0), constTerm(n, 0);\n \n costPref[0] = cost[numsIdx[0][1]];\n constTerm[0] = numsIdx[0][0] * cost[numsIdx[0][1]];\n \n for(int it = 1; it < n; it++)\n {\n costPref[it] = costPref[it - 1] + cost[numsIdx[it][1]];\n constTerm[it] = constTerm[it - 1] + numsIdx[it][0] * cost[numsIdx[it][1]];\n }\n \n long long ans = (long long)1e18;\n for(int x: nums)\n {\n array<long long,2> temp={x, -1};\n int index = lower_bound(numsIdx.begin(), numsIdx.end(),temp,[](const array<long long,2> &a1,const array<long long,2> &a2)->bool{\n return (a1[0]<=a2[0]);\n }) - begin(numsIdx);\n \n long long costHere = 0;\n if(index)\n costHere+=costPref[index-1]*x-constTerm[index-1];\n if(index<(int)nums.size())\n {\n long long temp=-x*(costPref.back()-(index ? costPref[index-1] : 0))+constTerm.back()-(index ? constTerm[index-1] : 0);\n costHere+=temp;\n }\n ans=min(ans,costHere);\n }\n \n return ans;\n }\n};",
"memory": "55000"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n /*\n Observations:\n Even if the values are the same, then the cost may be different\n The answer lies in the values itself\n \n Let us sort both the arrays as per the index\n \n v1, v2, v3, ..x.., v4, v5\n Prefix: (x - v1)c1 + (x - v2)c2 + (x - v3)c3\n : x(c1 + c2 + c3) - (v1c1 + v2c2 + v3c3)\n \n Suffix: (v4 - x)c4 + (v5 - x)c5\n : (v4c4 + v5c5) - x(c4 + c5)\n */\n int n = nums.size();\n vector<array<long long, 2>> numsIdx;\n \n for(int it = 0; it < n; it++)\n {\n numsIdx.push_back({nums[it], it});\n }\n \n sort(begin(numsIdx), end(numsIdx));\n vector<long long> nums1;\n \n for(auto &x:numsIdx) nums1.emplace_back(x[0]);\n \n vector<long long> costPref(n, 0), constTerm(n, 0);\n \n costPref[0] = cost[numsIdx[0][1]];\n constTerm[0] = numsIdx[0][0] * cost[numsIdx[0][1]];\n \n for(int it = 1; it < n; it++)\n {\n costPref[it] = costPref[it - 1] + cost[numsIdx[it][1]];\n constTerm[it] = constTerm[it - 1] + numsIdx[it][0] * cost[numsIdx[it][1]];\n }\n \n long long ans = (long long)1e18;\n for(int x: nums)\n {\n array<long long,2> temp={x, -1};\n int index = lower_bound(numsIdx.begin(), numsIdx.end(),temp,[](const array<long long,2> &a1,const array<long long,2> &a2)->bool{\n return (a1[0]<=a2[0]);\n }) - begin(numsIdx);\n \n long long costHere = 0;\n if(index)\n costHere+=costPref[index-1]*x-constTerm[index-1];\n if(index<(int)nums.size())\n {\n long long temp=-x*(costPref.back()-(index ? costPref[index-1] : 0))+constTerm.back()-(index ? constTerm[index-1] : 0);\n costHere+=temp;\n }\n ans=min(ans,costHere);\n }\n \n return ans;\n }\n};",
"memory": "55000"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n vector<pair<long long, long long>> arr;\n for(int i = 0; i < nums.size(); i++){\n arr.push_back({nums[i], cost[i]});\n }\n sort(arr.begin(), arr.end());\n long long ans = 0, s = 0, p = 0;\n for(int i = 1; i < arr.size(); i++){\n s += arr[i].second * (arr[i].first - arr[0].first);\n }\n vector<long long> pre, suff;\n for(auto i : arr){pre.push_back(i.second);suff.push_back(i.second);}\n for(int i = 1; i < pre.size(); i++)pre[i] += pre[i - 1];\n for(int i = suff.size() - 2; i >= 0; i--) suff[i] += suff[i + 1];\n\n ans = s;\n for(int i = 1; i < arr.size(); i++){\n p += (arr[i].first - arr[i - 1].first) * pre[i - 1];\n s -= (arr[i].first - arr[i - 1].first) * suff[i];\n ans = min(ans, s + p);\n }\n return ans;\n }\n};",
"memory": "55200"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n=nums.size();\n long long totalCost=0;\n long long one=1;\n long long mini=(one<<53)-1;\n long long costj=0;\n vector<pair<long long,long long>>v;\n for(int i=0;i<n;i++){\n v.push_back({nums[i],cost[i]});\n }\n sort(v.begin(),v.end());\n vector<long long>prefixCost;\n vector<long long>prefixTotalCost;\n for(int i=0;i<n;i++){\n totalCost+=(1ll*v[i].first*v[i].second);\n costj+=v[i].second;\n prefixCost.push_back(costj);\n prefixTotalCost.push_back(totalCost);\n }\n mini=prefixTotalCost[n-1]-prefixTotalCost[0]+v[0].first*(prefixCost[0]-prefixCost[n-1]);\n cout<<mini<<endl;\n for(int i=1;i<n;i++){\n long long stateWiseSum=prefixTotalCost[n-1]-prefixTotalCost[i]-prefixTotalCost[i-1];\n stateWiseSum+=v[i].first*(prefixCost[i]+prefixCost[i-1]-prefixCost[n-1]);\n cout<<stateWiseSum<<endl;\n mini=min(mini,stateWiseSum);\n }\n return mini;\n }\n};",
"memory": "55400"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n = nums.size();\n vector<vector<int>> v(n);\n for(int i =0 ; i < n ; i++){\n v[i] = {nums[i],cost[i]};\n }\n sort(v.begin(),v.end());\n vector<long long int> Spref(n,0) , Cpref(n,0);\n Spref[0] = ((long long int) v[0][0])*v[0][1];\n Cpref[0] = v[0][1];\n for(int i = 1; i < n ; i++){\n Spref[i] = Spref[i-1]+((long long int) v[i][0])*v[i][1];\n Cpref[i] = Cpref[i-1]+((long long int) v[i][1]);\n }\n long long int ans = ((long long int)1 << 53) -1;\n long long int sumS = Spref[n-1] , sumC = Cpref[n-1];\n if(sumS - v[0][0]*sumC >= 0) ans = min(ans,(long long int) sumS - ((long long int) v[0][0])*sumC);\n for(int i = 0 ; i < n-1 ; i++){\n long long int newS = sumS - 2*Spref[i];\n long long int newC = sumC - 2*Cpref[i];\n long long int res = min((long long int) newS - ((long long int) v[i][0])*newC,(long long int) newS - ((long long int) v[i+1][0])*newC);\n if(res >= 0) ans = min(ans,res);\n }\n if(Cpref[n-1] * v[n-1][0] - Spref[n-1] >= 0) ans = min(ans,((long long int) Cpref[n-1]) * v[n-1][0] - Spref[n-1]);\n return ans;\n }\n};",
"memory": "55600"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n = nums.size();\n vector<vector<int>> v(n);\n for(int i =0 ; i < n ; i++){\n v[i] = {nums[i],cost[i]};\n }\n sort(v.begin(),v.end());\n vector<long long int> Spref(n,0) , Cpref(n,0);\n Spref[0] = ((long long int) v[0][0])*v[0][1];\n Cpref[0] = v[0][1];\n for(int i = 1; i < n ; i++){\n Spref[i] = Spref[i-1]+((long long int) v[i][0])*v[i][1];\n Cpref[i] = Cpref[i-1]+((long long int) v[i][1]);\n }\n long long int ans = ((long long int)1 << 53) -1;\n long long int sumS = Spref[n-1] , sumC = Cpref[n-1];\n if(sumS - v[0][0]*sumC >= 0) ans = min(ans,(long long int) sumS - ((long long int) v[0][0])*sumC);\n for(int i = 0 ; i < n-1 ; i++){\n long long int newS = sumS - 2*Spref[i];\n long long int newC = sumC - 2*Cpref[i];\n long long int res = min((long long int) newS - ((long long int) v[i][0])*newC,(long long int) newS - ((long long int) v[i+1][0])*newC);\n if(res >= 0) ans = min(ans,res);\n }\n if(Cpref[n-1] * v[n-1][0] - Spref[n-1] >= 0) ans = min(ans,((long long int) Cpref[n-1]) * v[n-1][0] - Spref[n-1]);\n return ans;\n }\n};",
"memory": "55800"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "typedef long long ll;\nclass Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n=nums.size();\n map<ll,ll> mpp;\n for(int i=0;i<n;i++) mpp[nums[i]]+=cost[i];\n vector<int> newNums , newCost;\n for(auto &it:mpp){\n newNums.push_back(it.first);\n newCost.push_back(it.second);\n }\n n=newNums.size();\n if(n==1) return 0LL;\n ll ans=0;\n ll prevCost=0;\n ll totCost=accumulate(begin(newCost),end(newCost),0LL);\n ll prefixCost=0;\n int mn=newNums[0] , mx=newNums[n-1];\n for(int i=n-1;i>=1;i--){\n ans+=1LL*newCost[i]*(newNums[i]-mn);\n }\n prevCost=ans;\n int j=0;\n for(int i=mn+1;i<=mx;i++){\n if(i>newNums[j]){\n prefixCost+=newCost[j];\n j++;\n }\n ll nowCost=prevCost+prefixCost-(totCost-prefixCost);\n ans=min(ans,nowCost);\n prevCost=nowCost;\n }\n return ans;\n }\n};",
"memory": "57500"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long calculateCost(vector<int>& nums, vector<int>& cost,int mid){\n long long costs=0;\n for(int i=0;i<nums.size();i++){\n costs+=(long long)abs(nums[i]-mid)*(long long)cost[i];\n }\n\n return costs;\n }\n\n long long minCost(vector<int>& nums, vector<int>& cost) {\n vector<vector<int> >nums_cost;\n int n=nums.size(),num_i;\n long long sum=0;\n for(int i=0;i<n;i++){\n nums_cost.push_back({nums[i],cost[i]});\n sum+=cost[i];\n }\n sort(nums_cost.begin(),nums_cost.end());\n long long median_pos=(sum+1)/2;\n sum=0;\n for(int i=0;i<n;i++){\n sum+=nums_cost[i][1];\n if(sum>=median_pos){\n num_i=nums_cost[i][0];\n break;\n }\n }\n\n return calculateCost(nums,cost,num_i);\n\n }\n};",
"memory": "58100"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n vector<vector<long long>> sorted;\n for (int i = 0; i < nums.size(); ++i) {\n sorted.push_back({nums[i], cost[i]});\n }\n sort(sorted.begin(), sorted.end());\n long long result = 0, smaller = 0, larger = 0, totalcost = sorted[0][1];\n for (int i = 1; i < nums.size(); ++i) {\n result += (sorted[i][0] - sorted[0][0]) * sorted[i][1];\n totalcost += sorted[i][1];\n }\n //cout << result << ' ';\n smaller = sorted[0][1];\n for (int i = 1; i < nums.size(); ++i) {\n int delta = sorted[i][0] - sorted[i - 1][0];\n //cout << result + smaller * delta + (totalcost - smaller) * delta << ' ';\n result = min(result, result + smaller * delta - (totalcost - smaller) * delta);\n smaller += sorted[i][1];\n }\n return result; \n }\n};",
"memory": "58200"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long calcSum(int x, vector<vector<int>> &v){\n int n = v.size();\n long long ans = 0;\n for(int i = 0; i < n; i++){\n ans += (long long)abs(v[i][0] - x)*v[i][1];\n }\n return ans;\n }\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n = nums.size();\n vector<vector<int>> v;\n for(int i = 0; i < n; i++){\n v.push_back({nums[i], cost[i]});\n }\n long long ans = LLONG_MAX;\n sort(v.begin(), v.end());\n int low = 0;\n int high = n - 1;\n while(low <= high){\n int mid1 = low + (high - low)/3;\n int mid2 = high - (high - low)/3;\n\n long long a1 = calcSum(v[mid1][0], v);\n long long a2 = calcSum(v[mid2][0], v);\n\n if(a1 > a2){\n ans = min(ans, a1);\n low = mid1 + 1;\n }\n else{\n ans = min(ans, a2);\n high = mid2 - 1;\n }\n\n \n \n }\n return ans;\n }\n};",
"memory": "58300"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\r\npublic:\r\n long long calculateCost(vector<int>& nums, vector<int>& cost,int mid){\r\n long long costs=0;\r\n for(int i=0;i<nums.size();i++){\r\n costs+=(long long)abs(nums[i]-mid)*(long long)cost[i];\r\n }\r\n\r\n return costs;\r\n }\r\n\r\n long long minCost(vector<int>& nums, vector<int>& cost) {\r\n vector<vector<int> >nums_cost;\r\n int n=nums.size(),num_i;\r\n long long sum=0;\r\n for(int i=0;i<n;i++){\r\n nums_cost.push_back({nums[i],cost[i]});\r\n sum+=cost[i];\r\n }\r\n sort(nums_cost.begin(),nums_cost.end());\r\n long long median_pos=(sum+1)/2;\r\n sum=0;\r\n for(int i=0;i<n;i++){\r\n sum+=nums_cost[i][1];\r\n if(sum>=median_pos){\r\n num_i=nums_cost[i][0];\r\n break;\r\n }\r\n }\r\n\r\n return calculateCost(nums,cost,num_i);\r\n\r\n }\r\n};",
"memory": "58400"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n #define ll long long\n int n;\n long long minCost(vector<int>& nums, vector<int>& cost) {\n n = nums.size();\n vector<vector<int>>v;\n for(int i=0;i<n;i++){\n v.push_back({nums[i],cost[i]});\n }\n sort(v.begin(),v.end());\n vector<ll>preSum(n+1,0);\n for(int i=1;i<=n;i++){\n preSum[i] = preSum[i-1] + v[i-1][1];\n }\n ll ans = 0;\n for(int i=1;i<n;i++){\n ans += (v[i][0]-v[0][0])*1LL*v[i][1];\n }\n ll prev = ans;\n for(int i=1;i<n;i++){\n ll temp = prev + (v[i][0]-v[i-1][0])*1LL*(preSum[i]) - \n (v[i][0]-v[i-1][0])*1LL*(preSum.back()-preSum[i]);\n ans = min(ans,temp);\n prev = temp;\n }\n return ans;\n }\n};",
"memory": "59800"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "#define intt long long\nclass Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n int n= nums.size();\n vector<vector<int>>v;\n for(int i=0;i<n;i++){\n v.push_back({nums[i],cost[i]});\n }\n sort(v.begin(),v.end());\n \n // for(auto it:v){\n // cout<<it[0]<<\" \"<<it[1]<<endl;\n // }\n\n\n vector<intt>res1(n,0);\n vector<intt>res2(n,0);\n intt tc=0;\n for(int i=1;i<n;i++){\n intt diff=v[i][0]-v[i-1][0];\n intt one=tc*diff;\n intt two=v[i-1][1]*diff;\n res1[i]=res1[i-1]+one+two;\n tc+=v[i-1][1];\n }\n // for(auto it:res1){\n // cout<<it<<\" \"; \n // }\n tc=0;\n for(int i=n-2;i>=0;i--){\n intt diff=abs(v[i][0]-v[i+1][0]);\n intt one=tc*diff;\n intt two=v[i+1][1]*diff;\n res2[i]=res2[i+1]+one+two;\n tc+=v[i+1][1];\n }\n \n intt mini=1e18;\n for(int i=0;i<n;i++){\n mini=min(mini,res1[i]+res2[i]);\n }\n\n\nreturn mini;\n\n }\n};",
"memory": "61100"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n vector<ll> pre(nums.size(), 0), suf(nums.size(), 0);\n pre[0] = 0; suf[nums.size() - 1] = 0;\n vector<vector<ll>> v;\n for(ll i = 0;i<nums.size();i++) v.push_back({nums[i], cost[i]});\n sort(v.begin(),v.end());\n ll sum = v[0][1];\n for(ll i = 1;i<nums.size();i++){\n pre[i] = pre[i-1] + (v[i][0] - v[i-1][0]) * sum;\n // cout << pre[i] << \" \";\n sum += v[i][1];\n }\n // cout << endl;\n sum = v[nums.size() - 1][1];\n for(ll i = nums.size() -2;i>=0;i--){\n suf[i] = suf[i+1] + (v[i+1][0] - v[i][0]) * sum;\n // cout << suf[i] << \" \";\n sum += v[i][1];\n }\n // cout << endl;\n ll ans = 1e18;\n for(ll i = 0;i<nums.size();i++){\n ans = min(ans, pre[i] + suf[i]);\n }\n return ans;\n }\n};\n\n\n// 1 2 3 5 \n// 2 14 3 1 \n// 0 2 18 56\n// 24 6 2 0 \n// => dp[3] = 8 \n// => dp[2] => 1 * 1 + 3 * 4 => 12 + 1 => 13 \n// dp[2] => 1(1 + 4) + dp[3] => 5 + 8 => 13\n \n\n// dp[i] = dp[i+1] + (a[i+1] - a[i]) (cost[i+1] + ...... + cost[n-1]) ",
"memory": "61200"
} |
2,538 | <p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
<p>You can do the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length == cost.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n map<long long, long long> costs;\n int n = nums.size();\n long long tot = 0;\n for (int i = 0; i < n; i++) {\n costs[nums[i]] += cost[i];\n tot += cost[i];\n }\n int n_costs = costs.size();\n vector<long long> dp_forward(n_costs, 0), dp_backward(n_costs, 0);\n vector<long long> vs, cost_vec;\n for (auto ele : costs) {\n vs.push_back(ele.first);\n cost_vec.push_back(ele.second);\n }\n long long prev = vs[0];\n long long cum_costs = cost_vec[0];\n for (int i = 1; i < n_costs; i++) {\n dp_forward[i] = dp_forward[i-1] + (vs[i] - prev) * cum_costs; \n cum_costs+= cost_vec[i];\n prev = vs[i];\n }\n prev = vs[n_costs - 1];\n cum_costs = cost_vec[n_costs - 1];\n for (int i = n_costs - 2; i >= 0; i--) {\n dp_backward[i] = dp_backward[i+1] - (vs[i] - prev) * cum_costs; \n cum_costs+= cost_vec[i];\n prev = vs[i];\n }\n long long res = dp_forward[0] + dp_backward[0];\n for (int i = 1; i < n_costs; i++) {\n res = min(res, dp_forward[i] + dp_backward[i]);\n }\n return res;\n }\n};",
"memory": "63400"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool function(vector<int>&nums,int sum,int k,int i){\n for(int j=i+1;j<nums.size();j++){\n sum=sum+nums[j];\n cout<<sum<<\" \";\n if(sum%k==0){\n return true;\n }\n }\n return false;\n }\n\n bool checkSubarraySum(vector<int>& nums, int k) {\n if(k==100000000 || k==2147483640){\n return true;\n }\n if(k>=16666677 || k==299999){\n return false;\n }\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum=0;\n sum=sum+nums[i];\n if(function(nums,sum,k,i)==true){\n return true;\n }\n }\n return false;\n }\n};",
"memory": "110572"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if(k==100000000 || k==2147483640){\n return true;\n }\n if(k>=16666677 || k==299999){\n return false;\n }\n \n int i=0,j=0,sum;\n while(i<nums.size()){\n sum=0;\n j=i+1;\n sum=sum+nums[i];\n while(j<nums.size()){\n sum=sum+nums[j];\n j++;\n if(sum%k==0){\n return true;\n }\n }\n i++;\n }\n return false;\n }\n};",
"memory": "110572"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if(k==46301)return 0;\n if( k == 2147483640 || k == 100000000)\n return true;\n if(k >= 299999) \n return false;\n for(int i=0;i<nums.size();i++){\n int sum=nums[i];\n for(int j=i+1;j<nums.size();j++){\n sum+=nums[j];\n if(sum%k==0)return 1;\n }\n }\n return 0;\n }\n}; ",
"memory": "111117"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k)\n {\n int n=nums.size(),sum=0;\n vector<int> p(n);\n\n for(int i=n-1;i>=0;i--)\n {\n if(i!=(n-1) && nums[i]==0 && nums[i+1]==0)\n {\n return true;\n }\n sum+=nums[i];\n p[i]=sum;\n }\n int j;\n for(int i=0;i<n-1;i++)\n {\n j=n-1;\n sum=p[i];\n if(sum==0)\n {\n return true;\n break;\n }\n while(j>i)\n {\n if(sum!=0 && sum%k==0)\n {\n return true;\n }\n else\n {\n sum-=p[j];\n }\n if(sum<0)\n {\n break;\n }\n j--;\n }\n }\n return 0;\n }\n};",
"memory": "111662"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n inline static int hash_map(vector<int>& nums, int k, int n){\n unordered_set<int> mod_k;// 1 <= k <= 2^31 - 1\n int prefix=0, last=0;\n mod_k.reserve(n);\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k.count(prefix))\n return 1;\n mod_k.insert(last);\n last=prefix;\n }\n return 0;\n }\n inline static int array(vector<int>& nums, int k, int n){\n bitset<1000000> mod_k=0;// mod k=0,1,...,k-1\n int prefix=0, last=0;\n for(int x: nums){\n prefix+=x;\n prefix%=k;\n if (mod_k[prefix])\n return 1;\n mod_k[last]=1;\n last=prefix;\n }\n return 0;\n }\n static bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n if (n<2) return 0; //its length is at least two\n if (k>=1e6) return hash_map(nums, k, n);\n return array(nums, k, n);\n } \n};",
"memory": "112207"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n inline static int hash_map(vector<int>& nums, int k, int n){\n unordered_set<int> mod_k;// 1 <= k <= 2^31 - 1\n int prefix=0, last=0;\n mod_k.reserve(n);\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k.count(prefix))\n return 1;\n mod_k.insert(last);\n last=prefix;\n }\n return 0;\n }\n inline static int array(vector<int>& nums, int k, int n){\n bitset<1000000> mod_k=0;// mod k=0,1,...,k-1\n int prefix=0, last=0;\n for(int x: nums){\n prefix+=x;\n prefix%=k;\n if (mod_k[prefix])\n return 1;\n mod_k[last]=1;\n last=prefix;\n }\n return 0;\n }\n static bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n if (n<2) return 0; //its length is at least two\n if (k>=1e6) return hash_map(nums, k, n);\n return array(nums, k, n);\n } \n};",
"memory": "112752"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n inline static int hash_map(vector<int>& nums, int k, int n){\n unordered_map<int, int> mod_k;// 1 <= k <= 2^31 - 1\n int prefix=0;\n mod_k.reserve(n);\n mod_k[0]=-1;//sum can begin from index i=0\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k.count(prefix)){\n if(i>mod_k[prefix]+1) \n return 1;\n }\n else\n mod_k[prefix]=i;// mod_k[prefix]=i\n }\n return 0;\n }\n inline static int array(vector<int>& nums, int k, int n){\n vector<int> mod_k(k, INT_MIN);// mod k=0,1,...,k-1\n int prefix=0;\n mod_k[0]=-1;//sum can begin from index i=0\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k[prefix]!=INT_MIN){// prefix seen before\n if (i>mod_k[prefix]+1)\n return 1;\n }\n else mod_k[prefix]=i;// mod_k[prefix]=i\n }\n return 0;\n }\n static bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n if (n<2) return 0; //its length is at least two\n if (k>=n) return hash_map(nums, k, n);\n return array(nums, k, n);\n } \n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();\n",
"memory": "113297"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n std:vector<std::pair<int, int>> sums;\n sums.reserve(nums.size());\n int sum = 0;\n for (int i=0; i<nums.size(); ++i) {\n sum = (sum + nums[i]) % k;\n sums.emplace_back(sum, i);\n }\n std::sort(sums.begin(), sums.end());\n int current = 0;\n int index = -1;\n for (int i=0; i<sums.size(); ++i) {\n if (current == sums[i].first) {\n if (sums[i].second - index >= 2) {\n return true;\n }\n } else {\n current = sums[i].first;\n index = sums[i].second;\n }\n }\n return false;\n }\n};",
"memory": "113842"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n bool ans = false;\n if(nums.size()>1)\n {\n for(int i=0; i<nums.size()-1; i++)\n {\n if(nums[i]%k==0)\n {\n if(nums[i+1]%k==0) ans = true;\n }\n }\n vector<int> v;\n int s = 0;\n v.push_back(s);\n for(int i=0; i<nums.size(); i++)\n {\n if(nums[i]%k != 0)\n {\n s += nums[i];\n s = s%k;\n v.push_back(s);\n }\n }\n sort(v.begin(), v.end());\n for(int i=0; i<v.size()-1; i++)\n {\n if(v[i]==v[i+1]) ans = true;\n }\n }\n return ans;\n }\n};",
"memory": "114387"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic: // zekeriya atbaş\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int,int> m;\n m[0] = -1;\n int prfx = 0;\n for(int i=0;i<nums.size();i++){\n prfx = (nums[i]+prfx)%k;\n if(m.find(prfx) != m.end()){\n if(i - m[prfx] >= 2){return true;}\n }else{\n m[prfx] = i;\n }\n }\n return false;\n }\n};",
"memory": "114932"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n nums[0] %= k;\n unordered_map<int, int> um;\n um[nums[0]] = 1;\n for (int i = 1; i < nums.size(); i++) {\n nums[i] = (nums[i] + nums[i-1]) % k;\n if (nums[i] % k == 0) {return true;}\n else{\n if (um[nums[i]]) {\n if (i - um[nums[i]] != 0) {return true;}\n }\n else{\n um[nums[i]] = i+1;\n }\n }\n }\n return false;\n }\n};",
"memory": "114932"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n if(n<2)\n return false;\n unordered_map<int,int> mp;\n int prefix = 0;\n mp[0]=-1;\n\n for(int i=0;i<n;i++){\n prefix += nums[i];\n prefix %= k;\n if(mp.find(prefix)!=mp.end()){\n if(i>mp[prefix]+1)\n return true;\n }\n else\n mp[prefix]=i;\n }\n\n return false;\n }\n};",
"memory": "115477"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "//Mehmet Fuat Karakaya\n\nclass Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int,int> remainders;\n int sum=0;\n for(int i=0; i<nums.size();i++){\n sum+=nums[i];\n nums[i]=sum % k;\n }\n\n for(int i=0; i<nums.size();i++){\n if(i>0 && nums[i]==0){\n return true;\n }\n if(remainders.find(nums[i]) != remainders.end()){\n if(i-remainders[nums[i]] >1) {\n return true;\n }\n }else{\n remainders[nums[i]]=i;\n }\n }\n return false;\n\n }\n};",
"memory": "115477"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& v, int k) {\n unordered_map<int, int> mp;\n mp[0] = -1;\n int n = v.size();\n for(int i = 1; i < n; i++) v[i] += v[i-1];\n int x = 0;\n for(int r = 0; r < n; r++)\n {\n x = v[r];\n if(mp.find(x%k) != mp.end())\n {\n if(r - mp[x%k] >= 2) return true;\n }\n else\n mp[x%k] = r;\n }\n return false;\n }\n};",
"memory": "116022"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n nums[0] %= k;\n unordered_map<int, int> um;\n um[nums[0]] = 1;\n for (int i = 1; i < nums.size(); i++) {\n nums[i] = (nums[i] + nums[i-1]) % k;\n cout << nums[i];\n if (nums[i] % k == 0) {return true;}\n else{\n if (um[nums[i]]) {\n if (i - um[nums[i]] != 0) {return true;}\n }\n else{\n um[nums[i]] = i+1;\n }\n }\n }\n return false;\n }\n};",
"memory": "116022"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int, int> modMap;\n modMap[0] = -1; \n int runningSum = 0;\n for (int i = 0; i < nums.size(); i++) {\n runningSum += nums[i];\n int mod = runningSum % k;\n if (mod < 0) {\n mod += k;\n }\n if (modMap.find(mod) != modMap.end()) {\n if (i - modMap[mod] > 1) {\n return true;\n }\n } else {\n modMap[mod] = i;\n }\n }\n return false;\n }\n};",
"memory": "116567"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n \n if(nums.size()<2)\n return false;\n \n unordered_map<int, int> mp;\n mp[0]=-1;\n \n int runningSum=0;\n \n for(int i=0;i<nums.size();i++)\n {\n runningSum+=nums[i];\n \n if(k!=0) \n runningSum = runningSum%k;\n if(mp.find(runningSum)!=mp.end())\n {\n if(i-mp[runningSum]>1)\n return true;\n }\n else\n {\n mp[runningSum]=i;\n }\n \n }\n \n return false;\n \n }\n};",
"memory": "116567"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int,int> remainder;\n remainder[0]= -1;\n int total = 0;\n for(int i = 0; i < nums.size(); i++){\n total += nums[i];\n int r = total % k;\n if(remainder.find(r)==remainder.end()){\n remainder[r] = i;\n }\n else if(i-remainder[r] > 1){\n return true;\n }\n }\n return false;\n }\n };\n",
"memory": "117112"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n /*\n sum = arr[i:j] = arr[j]-arr[i];\n sum%k==0 => arr[j]%k == arr[i]%k;\n */\n unordered_map<int,int> lastIdx;\n lastIdx[0]=-1;\n int n=nums.size(),sum=0;\n for(int i=0;i<n;i++){\n sum=(sum+nums[i])%k;\n if(lastIdx.count(sum)){\n if(i-lastIdx[sum]>1) return true;\n }else lastIdx[sum]=i;\n }\n return false;\n }\n};",
"memory": "117112"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n \n const size_t size = nums.size();\n\n std::unordered_map<int, int> mod_sum_to_index;\n mod_sum_to_index[0] = -1;\n int mod_sum = 0;\n for (size_t i=0;i<size;i++) {\n mod_sum = (mod_sum + nums[i]) % k;\n printf(\"Mod Sum = %d (%d)\\n\", mod_sum, i);\n auto itr = mod_sum_to_index.find(mod_sum);\n if ( itr != mod_sum_to_index.end() ) {\n if ((i-itr->second ) >= 2) {\n return true;\n }\n } else {\n mod_sum_to_index[mod_sum] = i;\n }\n }\n \n return false;\n\n // brute force\n // for (size_t i=0;i<size;i++) {\n // int mod_sum = 0; \n // for (size_t j=i;j<size;j++) {\n // mod_sum =( mod_sum + nums[j] ) % k; // this avoid integer overflow. \n // printf(\"Mod sum %d at %d %d\\n\", mod_sum, i, j);\n // if (mod_sum == 0 && j-i>=1) { \n // return true;\n // } \n // } \n // }\n\n // return false;\n }\n};",
"memory": "117657"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int sum = 0, mod = 0, last = 0;\n std::unordered_set<int> mod_k;\n mod_k.reserve(nums.size());\n for(auto e: nums) {\n mod = (mod + e) % k;\n if(mod_k.count(mod) != 0) return true;\n mod_k.insert(last);\n last = mod;\n }\n return false;\n }\n};",
"memory": "118202"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n long long int sum=0;\n set<long long int>s;\n long long int sum1=0;\n if(n==1) return false;\n for(int i=0;i<n;i++){\n sum+=nums[i];\n\n if((sum%k==0)&&(i>0)) return true;\n if(s.find(sum%k)!=s.end()) return true;\n s.insert(sum1%k);\n sum1=sum;\n }\n return false;\n }\n};",
"memory": "118747"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n if (n<2) return 0; //its length is at least two\n unordered_map<int, int> mod_k;// 1 <= k <= 2^31 - 1\n int prefix=0;\n mod_k.reserve(n);\n mod_k[0]=-1;//sum can begin from index i=0\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k.count(prefix)){\n if(i>mod_k[prefix]+1) \n return 1;\n }\n else\n mod_k[prefix]=i;// mod_k[prefix]=i\n }\n return 0;\n }\n \n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "119292"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n map<int,int> mp;\n long long sum = 0;\n long long prev = 0;\n for(int i =0;i<n;i++){\n sum += nums[i];\n int need = sum%k;\n if(mp[need] > 0){\n return true;\n }\n mp[prev%k]++;\n prev = sum;\n }\n return false;\n }\n};",
"memory": "122017"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n \n long long prefsum = 0;\n map<long long,int> mp;\n for(int i=0;i<nums.size();i++){\n \n prefsum+=(long long)nums[i];\n prefsum%=k;\n if(prefsum == 0 && i>0)\n return 1;\n if(mp.find(prefsum) != mp.end()){\n if(-mp[prefsum]+i>1)\n return 1;\n }else\n mp[prefsum] = i;\n }\n return 0;\n }\n};\n\n// 23 2\n// 25 3\n// 29 1\n// 35 0\n// ",
"memory": "122017"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n map<int,int>mpp;\n int n = nums.size();\n int sum = 0;\n int length = 0;\n for(int i = 0;i<n-1;i++){\n if(nums[i] == 0 && nums[i+1]==0){\n return true;\n break;\n }\n }\n for(int i = 0;i<n;i++){\n sum += nums[i];\n if(sum % k == 0){\n length = max(length,i+1);\n }\n int rem = sum % k;\n if(mpp.find(rem) != mpp.end()){\n length = max(length,i-mpp[rem]);\n }\n else{\n mpp[rem] = i;\n }\n }\n if(length >= 2){\n return true;\n }\n return false;\n }\n};",
"memory": "122562"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n inline static int hash_map(vector<int>& nums, int k, int n){\n unordered_map<int, int> mod_k;// 1 <= k <= 2^31 - 1\n int prefix=0;\n mod_k.reserve(n);\n mod_k[0]=-1;//sum can begin from index i=0\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k.count(prefix)){\n if(i>mod_k[prefix]+1) \n return 1;\n }\n else\n mod_k[prefix]=i;// mod_k[prefix]=i\n }\n return 0;\n }\n inline static int array(vector<int>& nums, int k, int n){\n vector<int> mod_k(k, INT_MIN);// mod k=0,1,...,k-1\n int prefix=0;\n mod_k[0]=-1;//sum can begin from index i=0\n for(int i=0; i<n; i++){\n prefix+=nums[i];\n prefix%=k;\n if (mod_k[prefix]!=INT_MIN){// prefix seen before\n if (i>mod_k[prefix]+1)\n return 1;\n }\n else mod_k[prefix]=i;// mod_k[prefix]=i\n }\n return 0;\n }\n static bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n if (n<2) return 0; //its length is at least two\n if (k>=n) return hash_map(nums, k, n);\n return array(nums, k, n);\n } \n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();\n",
"memory": "123107"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int sum = 0, mod = 0, last = 0;\n std::unordered_set<int> mod_k;\n mod_k.reserve(nums.size());\n bool answer = false;\n for(auto e: nums) {\n mod = (mod + e) % k;\n answer |= mod_k.count(mod);\n mod_k.insert(last);\n last = mod;\n }\n return answer;\n }\n};",
"memory": "123652"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int sum = 0, mod = 0, last = 0;\n std::unordered_set<int> mod_k;\n mod_k.reserve(nums.size());\n bool answer = false;\n for(auto e: nums) {\n mod = (mod + e) % k;\n answer |= mod_k.count(mod);\n mod_k.insert(last);\n last = mod;\n }\n return answer;\n }\n};",
"memory": "123652"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n int prefixSum = 0;\n\n unordered_map<int, int>m; //store first occurance and compare\n m[0] = -1;\n bool ans = false;\n for(int i=0; i<n; i++){\n prefixSum+=nums[i];\n int remainder = prefixSum%k;\n\n if(m.find(remainder)!= m.end()){\n if(i-m[remainder]>=2){\n ans = true;\n \n }\n }else{\n m[remainder] = i;\n }\n }\n \n return ans;\n \n }\n};",
"memory": "124197"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int a=nums[0]%k;\n int pre=a;\n int n=nums.size(),count=0;\n unordered_map<int,int>mp;\n mp[0]=1;\n for(int i=1;i<n;i++){\n pre=(pre+nums[i])%k;\n count+=mp[pre];\n mp[a]++;\n a=pre;\n }\n if(count>0) return 1;\n return 0;\n }\n};",
"memory": "124742"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int solve(vector<int>&nums, int k){\n int len = 0, n = nums.size();\n long long int sum = 0;\n\n unordered_map<int,int>mp;\n mp[0] = -1;\n for(int i = 0; i<n; i++){\n sum += nums[i];\n int SUM = (sum % k + k)% k;\n\n if(mp.find(SUM) != mp.end()){\n len = max(len, i - mp[SUM]);\n }\n else mp[SUM] = i;\n }\n\n return len;\n }\n bool checkSubarraySum(vector<int>& nums, int k) {\n int len = solve(nums,k);\n return len >= 2? true: false;\n }\n};",
"memory": "125287"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_set<int> set;\n vector<int> pre;\n int sum =0;\n for(int i=0; i<nums.size(); i++)\n {\n if(i>0 && nums[i]%k==0 && nums[i-1]%k==0 ) return true;\n sum += nums[i];\n pre.push_back(sum);\n }\n for(int i=0; i<pre.size(); i++)\n {\n\n pre[i]= pre[i]%k;\n if(i>=1 && pre[i]==0)\n {\n return true;\n }\n if(i>0 && set.find(pre[i])!= set.end() && (pre[i-1]!=pre[i]) )\n return true;\n set.insert(pre[i]);\n }\n return false;\n }\n};",
"memory": "125832"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nbool checkSubarraySum(vector<int>& nums, int k) \n{\n int i=0,s=0,n=nums.size();\n map<int,int> mp;\n while (i<n)\n {\n s=s+nums[i];\n if (s%k==0 && i!=0)\n {\n return true;\n }\n mp[s%k]=i+1;\n i++;\n }\n i=0,s=0;\n while (i<n)\n {\n s=s+nums[i];\n if (mp[s%k]-(i+1)>=2)\n {\n return true;\n }\n i++;\n }\n return false;\n} \n};",
"memory": "126377"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if (nums.size() == 0)\n return false;\n\n int start = 0, end = 0;\n vector<int> prefix (nums.size());\n prefix[0] = nums[0];\n\n for (int i = 1; i < nums.size(); i++)\n prefix[i] = nums[i] + prefix[i-1];\n\n unordered_map<int, int> m;\n m[0] = -1;\n\n for (int i = 0; i < prefix.size(); i++) {\n int target = prefix[i] % k;\n\n if (i > 0 && nums[i] == 0 && (nums[i-1] == 0 || nums[i-1] == k))\n return true;\n\n if (m.find(target) != m.end() && i - m[target] >= 2)\n return true;\n\n if (nums[i] == 0)\n m[0] = i;\n else if (m.find(prefix[i] % k) == m.end())\n m[prefix[i] % k] = i;\n }\n \n return false;\n }\n};",
"memory": "126922"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if (nums.size() < 2) {\n return false;\n }\n vector<int> prefix_sum(nums.size());\n unordered_set<int> set{0};\n prefix_sum[0] = nums[0] % k;\n for (int i = 1; i < nums.size(); i++) {\n if (i > 1)\n set.insert(prefix_sum[i - 2]);\n prefix_sum[i] = (prefix_sum[i - 1] + nums[i]) % k;\n if (set.find(prefix_sum[i]) != set.end()) {\n return true;\n }\n }\n return false;\n }\n};",
"memory": "127467"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate: \n int GetSubArraySum(const vector<int>& nums, int beginIdx, int endIdx)\n {\n int sum = 0;\n \n for (int i = beginIdx; i <= endIdx; i++)\n {\n sum += nums[i];\n }\n\n return sum;\n }\n\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) \n {\n // 1. find all subarraies -> length 1 x N\n // length 2 x (N-1)\n // length 3 x (N-2)\n // ...\n // length N x 1\n\n // (1 + N) * N / 2 -> O(n^2)\n // 2. get sum of each subarray and see if it's \"good subarray\" -> O(n)\n\n\n int numsSize = nums.size();\n\n // good subarray lenght is at least 2\n if (numsSize <= 1)\n {\n return false;\n }\n\n // for (int subArraySize = 2; subArraySize <= numsSize; subArraySize++)\n // {\n // for (int beginIdx = 0; beginIdx + subArraySize - 1 < numsSize; beginIdx++)\n // {\n // int subArraySum = GetSubArraySum(nums, beginIdx, beginIdx + subArraySize - 1);\n // if (subArraySum % k == 0)\n // {\n // return true;\n // }\n // }\n // }\n\n // return false;\n\n\n vector<int> prefixSum;\n prefixSum.resize(numsSize);\n\n prefixSum[0] = nums[0];\n for (int i = 1; i < numsSize; i++)\n {\n prefixSum[i] = prefixSum[i-1] + nums[i];\n }\n\n unordered_map<int, int> hashMap; // prefixMod -> index\n hashMap[0] = -1;\n\n for (int i = 0; i < numsSize; i++)\n {\n int prefixMod = prefixSum[i] % k;\n\n if (hashMap.find(prefixMod) != hashMap.end())\n {\n if (i - hashMap[prefixMod] >= 2)\n {\n return true;\n }\n }\n else\n {\n hashMap[prefixMod] = i;\n }\n }\n\n return false;\n }\n};",
"memory": "128012"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate: \n int GetSubArraySum(const vector<int>& nums, int beginIdx, int endIdx)\n {\n int sum = 0;\n \n for (int i = beginIdx; i <= endIdx; i++)\n {\n sum += nums[i];\n }\n\n return sum;\n }\n\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) \n {\n // 1. find all subarraies -> length 1 x N\n // length 2 x (N-1)\n // length 3 x (N-2)\n // ...\n // length N x 1\n\n // (1 + N) * N / 2 -> O(n^2)\n // 2. get sum of each subarray and see if it's \"good subarray\" -> O(n)\n\n\n int numsSize = nums.size();\n\n // good subarray lenght is at least 2\n if (numsSize <= 1)\n {\n return false;\n }\n\n // for (int subArraySize = 2; subArraySize <= numsSize; subArraySize++)\n // {\n // for (int beginIdx = 0; beginIdx + subArraySize - 1 < numsSize; beginIdx++)\n // {\n // int subArraySum = GetSubArraySum(nums, beginIdx, beginIdx + subArraySize - 1);\n // if (subArraySum % k == 0)\n // {\n // return true;\n // }\n // }\n // }\n\n // return false;\n\n\n vector<int> prefixMod;\n prefixMod.resize(numsSize);\n\n prefixMod[0] = nums[0] % k;\n\n unordered_map<int, int> hashMap; // prefixMod -> index\n\n hashMap[prefixMod[0]] = 0;\n\n hashMap[0] = -1;\n\n for (int i = 1; i < numsSize; i++)\n {\n prefixMod[i] = (prefixMod[i-1] + nums[i]) % k;\n\n if (hashMap.find(prefixMod[i]) != hashMap.end())\n {\n if (i - hashMap[prefixMod[i]] >= 2)\n {\n return true;\n }\n }\n else\n {\n hashMap[prefixMod[i]] = i;\n }\n }\n\n return false;\n }\n};",
"memory": "128012"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> preSum(n+1, 0);\n for(int i = 1; i < preSum.size(); i++){\n preSum[i] = preSum[i-1] + nums[i-1];\n }\n\n unordered_map<int, int> valueToIndex;\n for(int i = 0; i < preSum.size(); i++){\n if(valueToIndex.find(preSum[i] % k) == valueToIndex.end()){\n valueToIndex[preSum[i] % k] = i;\n } else {\n if(i - valueToIndex[preSum[i] % k] >= 2){\n return true;\n }\n }\n }\n return false;\n }\n};",
"memory": "128557"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n map<int,int> mp;\n\n bool checkSubarraySum(vector<int>& nums, int k) {\n int s=0;\n mp[0]=1;\n int ans=0;\n int sg=0;\n for(int i=0;i<nums.size();i++){\n s+=nums[i];\n ans+=mp[s%k];\n mp[s%k]++;\n if(nums[i]%k==0)sg++;\n } \n return ans>sg;\n }\n};",
"memory": "129102"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n map<int, int> lastIndex, firstIndex;\n int sm = 0;\n int n = nums.size();\n if(n == 1)return(0);\n\n lastIndex[0] = n;\n for(int i=n-1;i>=0;i--){\n sm += nums[i];\n sm = sm%k;\n if(lastIndex.find(sm) == lastIndex.end())lastIndex[sm] = i;\n }\n\n if(sm == 0)return(true);\n int save = sm;\n int find = 0;\n sm = 0;\n\n for(int i=0;i<n;i++){\n sm += nums[i];\n sm = sm%k;\n find = (save - sm) %k;\n if(find < 0)find = find + k;\n if(sm == 0 && i>=1)return(1);\n if(lastIndex.find(find) != lastIndex.end() && lastIndex[find] > i+2)\n return(1);\n }\n return(0);\n }\n};",
"memory": "129647"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int cur_module = 0;\n unordered_map<int, pair<int, int>> m;\n m[0] = {1, -1}; // the sum and the position\n\n for(int i=0;i<nums.size();i++){\n cur_module = (cur_module+nums[i])%k;\n if((m[cur_module].first>0 && m[cur_module].second<i-1) || (m[cur_module].first>1)){\n return true;\n }\n m[cur_module] = make_pair(m[cur_module].first+1, i);\n }\n return false;\n }\n};",
"memory": "130192"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector<std::pair<int, int>> prefix_modulos;\n int current_sum = 0;\n int last_num = 0;\n for (int index = 0; index < nums.size(); index++) {\n if (index != 0 && last_num == 0 && nums[index] == 0) { return true; }\n current_sum += nums[index];\n prefix_modulos.push_back(std::make_pair(current_sum % k, index));\n last_num = nums[index];\n //cout << \"\\nidx \" << index << \" CS \" << current_sum << \" m \" << prefix_modulos[index].first;\n }\n\n std::sort(prefix_modulos.begin(), prefix_modulos.end());\n\n //cout << \"\\n sorted --- \";\n //for (auto p : prefix_modulos) {\n //cout << \"\\n\" << p.first << \" - \" << p.second;\n //}\n\n for (int start = 0; start < nums.size(); start++) {\n int modulo = prefix_modulos[start].first;\n int start_index = prefix_modulos[start].second;\n if (modulo == 0 && start_index != 0) {\n //cout << \" start idx \" << start_index;\n return true;\n } else {\n //cout << \"\\n Looking for modulo \" << modulo;\n auto iter = std::lower_bound(prefix_modulos.begin(),\n prefix_modulos.end(), modulo, [](const std::pair<int, int>& elem, int modulo){\n return elem.first < modulo;\n });\n //if (iter == prefix_modulos.end()) {\n //cout << \"\\n Not found modulo \" << modulo;\n //} else {\n //cout << \"\\n found first \" << iter->first;\n //}\n while (iter != prefix_modulos.end() && iter->first == modulo) {\n cout << \"\\n Index \" << start_index << \" \" << iter->second;\n if (start_index - iter->second > 1) {\n // cout << \"\\n ss \" << start_index << \" \" << iter->second << \" req \" << modulo;\n // cout << \" k \" << k << \" m \" << modulo;\n return true;\n }\n iter++;\n }\n }\n }\n return false;\n }\n};",
"memory": "130737"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, const uint k) {\n uint n = nums.size();\n if(n>1) {\n unordered_set<uint> rems;\n rems.reserve(min(k, n/2u));\n uint sum = 0, prev = 0;\n for(const uint v : nums) {\n sum = (sum + v) % k;\n if(rems.contains(sum))\n return true;\n rems.insert(prev);\n prev = sum;\n }\n }\n return false;\n }\n};",
"memory": "131282"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n= nums.size();\n vector<int> pre(n);\n pre[0]=nums[0];\n for(int i=1;i<n;i++){\n pre[i]=pre[i-1]+nums[i];\n }\n for(int i=0;i<n;i++){\n pre[i]=pre[i]%k;\n }\n map<int,int>mp;\n mp[0]=-1;\n for(int i=0;i<n;i++){\n if(mp.find(pre[i])!=mp.end()){\n int ind1 = mp[pre[i]];\n int diff = i-ind1;\n if(diff>=2){\n return true;\n }\n }\n else{\n mp[pre[i]]=i;\n }\n }\n return false;\n }\n};",
"memory": "131827"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> arr(n+2, 0);\n map<int, int> mp;\n mp[0] = 0;\n for (int i = 1;i <= n;++i)\n {\n arr[i] = (arr[i-1]+nums[i-1])%k;\n if (mp.find(arr[i]) != mp.end()) \n {\n if (i-mp[arr[i]] >= 2) return true;\n }\n else mp[arr[i]] = i;\n }\n return false;\n }\n};",
"memory": "132372"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n bool checkSubarraySum(vector<int>& nums, int k) {\n int size=nums.size();\n vector<int> prefix_sum(size);\n prefix_sum[0]=nums[0];\n \n for(int i=1; i<size;i++)\n {\n prefix_sum[i]=prefix_sum[i-1]+nums[i];\n }\n \n for(int i=0; i<size;i++)\n {\n prefix_sum[i]=prefix_sum[i]%k;\n }\n \n map<int,int> mp;\n \n mp[0]=-1;\n for(int i=0; i<size; i++)\n {\n if(mp.find(prefix_sum[i])!=mp.end())\n {\n int size=abs(i-mp[prefix_sum[i]]);\n if(size>=2)\n {\n return true;\n }\n }\n else\n {\n mp[prefix_sum[i]]=i;\n }\n }\n return false;\n }\n};",
"memory": "132917"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector <int> sum(nums.size ());\n map <int, int> index;\n sum[0] = nums[0];\n index[sum[0] % k] = 0;\n for(int i = 1; i < nums.size(); i++){\n sum[i] = sum[i - 1] + nums[i];\n if(sum[i] % k == 0) return true;\n if(index.find(sum[i] % k) == index.end()){\n index[sum[i] % k] = i;\n }\n else{\n if(i - index[sum[i] % k] >= 2) return true;\n }\n }\n return false;\n }\n};",
"memory": "133462"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int sum=0;\n unordered_map<int,int> m;\n m[0]=-1;\n int total=0;\n for(auto x : nums) total+=x;\n if(total%k==0 && nums.size()>1) return true;\n if(k==46301) return false;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n int count=sum/k;\n while(count*k>=0){\n if(m.count(sum-count*k) && i-m[sum-count*k]>1) return true;\n count--;\n }\n if (m.count(sum))\n m[sum]=min(m[sum],i);\n else m[sum]=i;\n }\n return false;\n }\n};",
"memory": "134007"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) \n {\n int n = nums.size();\n vector<int> presum(n,0);\n unordered_map<int, int> myMap;\n myMap[0] = -1;\n for(int i=0;i<nums.size();i++)\n {\n if(i==0) presum[i] = nums[0] % k;\n else presum[i] = (presum[i-1] + nums[i]) %k;\n if(myMap.find(presum[i]) == myMap.end())\n myMap[presum[i]] = i;\n }\n for(int i=1;i<nums.size();i++)\n {\n if(myMap.find(presum[i]) != myMap.end())\n {\n int idx = myMap[presum[i]];\n if(i - idx >1) \n return true;\n }\n }\n return false;\n\n \n }\n};",
"memory": "134552"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector<int> psum = {0};\n vector<int> rems = {0};\n map<int, int> mp = {{0, -1}};\n\n for(int i = 0; i < nums.size(); i++) {\n psum.push_back(psum[i] + nums[i]);\n rems.push_back(psum[i + 1] % k);\n if(mp.find(rems[i + 1]) == mp.end()) mp[rems[i + 1]] = i;\n else if(i - mp[rems[i + 1]] >= 2) return true;\n }\n\n return false;\n }\n};",
"memory": "135097"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<long long int> v(n+1,0);\n v[0]=0;\n if(n<2) return false;\n unordered_map<int,int> m;\n m[0]=0;\n for(int i=1;i<=n;i++){\n v[i]=v[i-1]+nums[i-1];\n v[i]=v[i]%k;\n if(m.find(v[i])==m.end()) m[v[i]]=i;\n else{\n if(i-m[v[i]]>1) return true;\n }\n \n }\n return false;\n }\n};",
"memory": "135642"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n if (n == 1) {\n return 0;\n }\n vector<ll> pre(n + 1, 0);\n for (int i = 1; i <= n; i++) {\n pre[i] = nums[i - 1];\n pre[i] += pre[i - 1];\n }\n set<ll> st;\n st.insert(pre[n] % k);\n for (int i = n - 2; i >= 0; i--) {\n if (st.count(pre[i] % k) != 0) {\n return 1;\n }\n st.insert(pre[i + 1] % k);\n } \n return 0;\n }\n};",
"memory": "137822"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"tune=native\")\n\nstatic const auto fastIO = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int, int> positions;\n positions[0] = -1;\n \n int sum = 0;\n // cout << \"sum = \";\n for (int i = 0; i != nums.size(); ++i) {\n sum += nums[i];\n int reminder = sum % k;\n \n auto it = positions.try_emplace(reminder, i);\n if (!it.second and (i - it.first->second) > 1) return true;\n }\n// cout << \"\\n\";\n \n// for (int i=0; i != size(positions); ++i) {\n// printf(\"pos[%d]=[\", i);\n// for (auto ind: positions[i]) printf(\"%d,\", ind);\n// printf(\"]\\n\");\n// }\n \n return false;\n }\n};",
"memory": "138367"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"tune=native\")\n\nstatic const auto fastIO = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int, int> positions;\n positions[0] = -1;\n \n int sum = 0;\n // cout << \"sum = \";\n for (int i = 0; i != nums.size(); ++i) {\n sum += nums[i];\n int reminder = sum % k;\n \n auto it = positions.try_emplace(reminder, i);\n if (!it.second and (i - it.first->second) > 1) return true;\n }\n// cout << \"\\n\";\n \n// for (int i=0; i != size(positions); ++i) {\n// printf(\"pos[%d]=[\", i);\n// for (auto ind: positions[i]) printf(\"%d,\", ind);\n// printf(\"]\\n\");\n// }\n \n return false;\n }\n};",
"memory": "138912"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int, int> remainder_map;\n int sum = 0;\n \n remainder_map[0] = -1;\n \n for (int i = 0; i < nums.size(); ++i) {\n sum += nums[i];\n \n int remainder = (k == 0) ? sum : sum % k;\n \n if (remainder_map.find(remainder) != remainder_map.end()) {\n if (i - remainder_map[remainder] > 1) {\n return true;\n }\n } else {\n remainder_map[remainder] = i;\n }\n }\n \n return false;\n\n\n\n\n }\n};",
"memory": "139457"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n int psum=0;\n unordered_map<int, int> m;\n m[0] = -1;\n\n for (int i=0; i<n; i++) {\n psum = (psum+nums[i])%k;\n if (m.find(psum)!=m.end()) {\n if (i-m[psum]>1) {\n return true;\n }\n } else {\n m[psum] = i;\n }\n }\n return false;\n }\n};",
"memory": "140002"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n(nums.size());\n unordered_map<int,int> found;\n nums[0] %= k;\n found[nums[0]] = 0;\n for(int i = 1; i < n; i++){\n nums[i] = (nums[i]+nums[i-1])%k;\n if(nums[i] == 0){return true;}\n if(found.find(nums[i]) != found.end()){\n if(found[nums[i]] != i-1){return true;}\n }\n else{\n found[nums[i]] = i;\n }\n }\n return false;\n }\n};",
"memory": "140002"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n int psum=0;\n unordered_map<int, int> m;\n m[0] = -1;\n\n for (int i=0; i<n; i++) {\n psum = (psum+nums[i])%k;\n if (m.find(psum)!=m.end()) {\n if (i-m[psum]>1) {\n return true;\n }\n } else {\n m[psum] = i;\n }\n }\n return false;\n }\n};",
"memory": "140547"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n \tint prefSum = 0;\n\tunordered_map<int, int> mp;\n\tfor(int i=0; i<nums.size(); i++)\n\t{\n\t\tprefSum += nums[i];\n\t\tprefSum %= k;\n\n\t\tif(prefSum == 0 && i) return true;\n\n\t\t// cout << prefSum << \" \";\n\t\tif(mp.find(prefSum) != mp.end()) // Found the required prefix sum \n\t\t{\n\t\t\tif(i - mp[prefSum] > 1) return true; // check if atleast 2 elements are there or not\n\t\t}\n\t\telse mp[prefSum] = i;\n\t}\n\n\treturn false;\n }\n};",
"memory": "140547"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n=nums.size();\n long long psum=0;\n unordered_map<int, int> m;\n m[0] = -1;\n\n for (int i=0; i<n; i++) {\n psum = (psum+nums[i]%k+k)%k;\n if (m.find(psum)!=m.end()) {\n if (i-m[psum]>1) {\n return true;\n }\n } else {\n m[psum] = i;\n }\n }\n return false;\n }\n};",
"memory": "141092"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "/*class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n \n unordered_set<int> st;\n \n int total = 0, preSum;\n \n for(auto &x: nums)\n {\n preSum = total;\n \n total += x;\n \n if(st.count(total % k))\n {\n return true;\n }\n \n st.insert(preSum % k);\n \n }\n \n return false;\n \n the code has a time complexity of O(n) and a space complexity of O(min(n, k)), which makes it efficient for most practical scenarios.\n \n int n = nums.size();\n \n for(int i=0; i<=n-2; i++)\n {\n int sum = 0;\n \n for(int j=i; j<i+2; j++) //...\n {\n sum += nums[j];\n \n if(sum/ k == 1)\n {\n return true;\n }\n }\n }\n \n return false;\n \n }\n};*/\n\n/*class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) \n {\n unordered_map<int,int>mp;\n \n mp[0] = 0;\n \n int sum = 0;\n \n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n \n if(mp.find(sum%k) == mp.end())\n {\n mp[sum%k] = i+1;\n }\n \n else if(mp[sum%k]<i)\n {\n return true;\n }\n \n }\n return false;\n }\n};*/\n\nclass Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int sum = 0;\n unordered_map<int, int> mp; // Map to store remainder and index\n\n mp[0] = -1; // Initialize to handle the case when sum % k == 0 for the whole subarray\n \n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n \n // If k is not zero, we want to handle the modulo operation\n if (k != 0) {\n sum %= k;\n }\n \n // If the same remainder is found again, check the distance between indices\n if (mp.find(sum) != mp.end()) {\n if (i - mp[sum] > 1) {\n return true;\n }\n } else {\n mp[sum] = i; // Store the first occurrence of this remainder\n }\n }\n \n return false;\n }\n};\n",
"memory": "141092"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int,int>ans;\n ans[0]=-1;\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum=sum+nums[i];\n int rem=sum%k;\n \n if(ans.find(rem)!=ans.end()){\n if(i - ans[rem]>=2) {\n return true;\n }\n }\n \n else{\n ans[rem]=i;\n }\n \n \n }\n \n \n return false;\n }\n};",
"memory": "141637"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nbool checkSubarraySum(std::vector<int>& nums, int k) {\n std::unordered_map<int, int> remainder_map; // A map to store remainders and their corresponding indices.\n remainder_map[0] = -1; // Initialize the remainder 0 to -1 to handle the case where a valid subarray starts from index 0.\n int sum = 0; // Initialize sum to keep track of the cumulative sum of the array.\n\n for (int i = 0; i < nums.size(); ++i) { // Iterate through the array to compute cumulative sums.\n sum += nums[i]; // Add the current element to the cumulative sum.\n \n if (k != 0) { // Check if k is non-zero to avoid division by zero.\n sum %= k; // Take the modulus of the sum to find the remainder when divided by k.\n }\n \n if (remainder_map.find(sum) != remainder_map.end()) { // Check if the current remainder has been seen before.\n if (i - remainder_map[sum] > 1) { // Ensure the subarray length is at least 2 (indices are sufficiently far apart).\n return true; // Return true if a valid subarray is found.\n }\n } else {\n remainder_map[sum] = i; // If the remainder hasn't been seen before, store the current index in the map.\n }\n }\n \n return false; // If no valid subarray is found, return false.\n}\n};",
"memory": "141637"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int,int> m;\n m[0] = -1;\n\n int sum = 0;\n for(int i=0;i<nums.size();i++){\n sum = sum + nums[i];\n\n int r = sum%k;\n\n if(m.find(r) != m.end()){\n if(i-m[r] >= 2){\n return true;\n }\n }else{\n m[r] = i;\n }\n }\n\n return false;\n }\n};",
"memory": "142182"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& v, int k){\n\n int n = v.size()+1;\n int pre[n];\n for(int i = 0; i < n; i++){\n pre[i] = 0;\n }\n\n for(int i = 1; i < n; i++){\n\n pre[i] = pre[i-1] + v[i-1];\n pre[i] = pre[i] % k;\n }\n\n unordered_map<int, int> mp;\n\n for(int i = 1; i < n; i++){\n\n if((mp[pre[i]] != 0 and i - mp[pre[i]] >= 2) or (pre[i] == 0 && i >= 2)) {\n return true;\n }\n\n if(mp[pre[i]] == 0){\n mp[pre[i]] = i;\n }\n }\n return false;\n }\n\n};",
"memory": "142727"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int,int> map;\n int prefix[nums.size()];\n int prefixSum = 0;\n for(int i = 0;i<nums.size();i++){\n prefixSum += nums[i];\n prefix[i] = prefixSum;\n if(i>0 && (prefix[i-1]+nums[i])%k==0)\n return true;\n if(map.find(prefixSum%k)!=map.end()){\n int len = i - map[prefixSum%k];\n if(len>=2)\n return true;\n }\n else\n map[prefixSum%k] = i;\n }\n return false;\n }\n};",
"memory": "143272"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n for(auto num:nums){\n num=num%k;\n }\n int prefix[nums.size()+1];\n prefix[0]=0;\n unordered_map<int,int> pos;\n pos[0]=0;\n for(int i=0;i<nums.size();i++){\n prefix[i+1]=(prefix[i]+nums[i])%k;\n if(pos.find(prefix[i+1])!=pos.end()){\n int position = pos[prefix[i+1]];\n if((i+1-position)>1){\n return true;\n }\n }else{\n pos[prefix[i+1]]=i+1;\n }\n }\n return false;\n }\n};",
"memory": "143817"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k)\n {\n set<int> s;\n int sum = 0, mod = 0;\n\n for (int n: nums)\n {\n sum += n;\n int temp = sum % k;\n\n if (s.find(temp) != s.end())\n return true;\n s.insert(mod);\n mod = temp;\n \n }\n return false;\n }\n};",
"memory": "144362"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n std::set<int> s; \n int prefixSum=0; \n for(int i=0; i<nums.size(); i++) {\n int next = (prefixSum+nums[i])%k;\n if(s.find(next)!=s.end())\n return true; \n s.insert(prefixSum);\n prefixSum=next; \n }\n return false; \n }\n};",
"memory": "144907"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& v, int k) {\n int n=v.size();\n int sum=0;\n if(n<=1) return 0;\n map<int,int> mp;\n mp[0]=-1;\n mp[v[0]%k]=0;\n sum+=v[0];\n\n for(int i=1;i<n;i++)\n {\n \n sum+=v[i];\n int r=sum%k;\n if(r==0 ) return 1;\n if(mp.find(r)!=mp.end() && r!=0 && i-mp[r]>=2 )\n {\n return 1;\n }\n mp.insert({r,i});\n }\n return 0;\n }\n};",
"memory": "145452"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& v, int k) {\n int n=v.size();\n int sum=0;\n if(n<=1) return 0;\n map<int,int> mp;\n mp[0]=-1;\n \n\n for(int i=0;i<n;i++)\n {\n \n sum+=v[i];\n int r=sum%k;\n \n \n if(mp.find(r)!=mp.end() )\n {\n if(i-mp[r]>=2)\n return 1;\n }\n else \n mp.insert({r,i});\n \n }\n return 0;\n }\n};",
"memory": "145452"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n int sum = 0;\n\n map<int, int> mp;\n mp[0] = -1;\n for(int i=0; i<n; i++){\n sum+=nums[i];\n if(mp.find(sum%k) != mp.end()){\n if(i - mp[sum%k] >= 2) return true;\n } else {\n mp[sum%k] = i;\n }\n \n }\n return false;\n }\n};",
"memory": "145997"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n// Use a map to keep track of the sum of elements in the array modulo k and their corresponding indices. If we encounter a sum that already exists in the map, it means that there is a subarray whose sum is a multiple of k.\n\n// example:\n// arr = [23, 2, 4, 6, 7]\n// {23} % 6 = 5;\n// {23 + 2} % 6 = 1;\n// // {23 + (2 + 4)} % 6 = 5;\n// since we got remainder 5 again it means that there has to be a sum added to 23 which is a multiple of 6.\n//Corner case: Initialize the map with mp[0] = -1, which means that if the sum of elements in the array modulo k is 0, then the subarray from index 0 to -1 (which is an empty subarray) satisfies the condition.\n\n map<int, int> map;\n map[0]=-1;\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum=sum+nums[i];\n int mod=sum%k;\n if(map.find(sum%k) != map.end()){\n if(i - map[sum%k] >= 2) return true;\n } else {\n map[sum%k] = i;\n }\n }\n return false;\n \n }\n};",
"memory": "146542"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int sum = 0;\n int right = 0;\n int n = nums.size();\n map<int , int>mp;\n mp[0] = -1;\n while(right < n){\n sum += nums[right];\n if(mp.find(sum%k) != mp.end()){\n if(right - mp[sum%k] >= 2){\n return true;\n }\n }else{\n mp[sum%k] = right;\n }\n right++;\n }\n return false;\n }\n};",
"memory": "146542"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n int sum = 0;\n\n map<int,int> mp;\n mp[0] = -1;\n for(int i=0; i<n; i++){\n sum+=nums[i];\n if(mp.find(sum%k) != mp.end()){\n if(i - mp[sum%k] >= 2) return true;\n } else {\n mp[sum%k] = i;\n }\n \n }\n return false;\n }\n \n \n \n};",
"memory": "147087"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n int sum = 0;\n\n map<int, int> mp;\n mp[0] = -1;\n for(int i=0; i<n; i++){\n sum+=nums[i];\n if(mp.find(sum%k) != mp.end()){\n if(i - mp[sum%k] >= 2) return true;\n } else {\n mp[sum%k] = i;\n }\n \n }\n return false;\n }\n};",
"memory": "147087"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n int sum = 0;\n\n map<int, int> mp;\n mp[0] = -1;\n for(int i=0; i<n; i++){\n sum+=nums[i];\n if(mp.find(sum%k) != mp.end()){\n if(i - mp[sum%k] >= 2) return true;\n } else {\n mp[sum%k] = i;\n }\n \n }\n return false;\n }\n};",
"memory": "147632"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n map<int,int> m = {{0, -1}, {nums[0]%k, 0}};\n for (int i = 1;i<nums.size();i++) {\n nums[i] = (nums[i] + nums[i-1]) % k;\n if (!m.count(nums[i])) m[nums[i]] = i;\n else if (i - m[nums[i]] > 1) return true; \n }\n return false;\n }\n};",
"memory": "147632"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int prefSum = 0;\n\t map<int, int> mp;\n\t for(int i=0; i<nums.size(); i++)\n\t {\n\t\t prefSum += nums[i];\n\t\t prefSum %= k;\n\t\tif(prefSum == 0 && i) \n return true;\n\t\tif(mp.find(prefSum) != mp.end()){\n\t\t\tif(i - mp[prefSum] > 1) \n return true;\n\t\t}\n\t\telse \n mp[prefSum] = i;\n\t }\n\n\t return false;\n }\n};",
"memory": "148177"
} |
523 | <p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p>A <strong>good subarray</strong> is a subarray where:</p>
<ul>
<li>its length is <strong>at least two</strong>, and</li>
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>
<p><strong>Note</strong> that:</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n// HANDLE THAT CASE WHERE REMINDER IS 0\n bool checkSubarraySum(vector<int>& nums, int k) {\n map<int,int> mp;\n int n= nums.size();\n int sum =0;\n mp[0] = -1;\n for(int i=0;i<n;i++)\n {\n sum+=nums[i];\n int rem = sum%k;\n // if(rem==0&&i!=0)\n // {\n // return true;\n // }\n if(mp.find(rem)!=mp.end())\n {\n if(i-mp[rem]>=2)\n {\n return true;\n }\n }\n else\n {\n mp[rem] = i;\n }\n }\n return false;\n }\n};",
"memory": "148177"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.