From 0446c8ed94f3abe7b23ebb8895ff4820b5716ba4 Mon Sep 17 00:00:00 2001 From: parikhitritgithub Date: Mon, 10 Jun 2024 11:09:21 +0530 Subject: [PATCH 01/11] add all --- .../lc-solutions/0500-0599/523. Continuous Subarray Sum.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md new file mode 100644 index 000000000..e69de29bb From 3b7da283c00430e7c9ac291af38080ec09deefa4 Mon Sep 17 00:00:00 2001 From: parikhitritgithub Date: Mon, 10 Jun 2024 11:14:15 +0530 Subject: [PATCH 02/11] add all --- .../0500-0599/523. Continuous Subarray Sum.md | 200 ++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index e69de29bb..5a4f2323a 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -0,0 +1,200 @@ +--- +id: Continuous Subarray Sum +title: Continuous Subarray Sum +sidebar_label: 0523 Continuous Subarray Sum +tags: + - prefix sum + hashmap + - LeetCode + - Java + - Python + - C++ +description: "This is a solution to the Continuous Subarray Sum problem on LeetCode." +--- + +## Problem Description + +Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise. +A good subarray is a subarray where: + + - its length is at least two, and + - the sum of the elements of the subarray is a multiple of k. +Note that: + - A subarray is a contiguous part of the array. + - An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k. + +### Examples + +**Example 1:** + +``` + + +Input: nums = [23,2,4,6,7], k = 6 +Output: true +Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. + +``` + +**Example 2:** + + +``` +Input: root = nums = [23,2,6,4,7], k = 6 +Output: true +Explanation: [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. +``` + +**Example 3:** + + +``` +Input: nums = [23,2,6,4,7], k = 13 +Output: false +``` + + +### Constraints + +- 1 <= nums.length <= 105 +- 0 <= nums[i] <= 109 +- 0 <= sum(nums[i]) <= 231 - 1 +- 1 <= k <= 231 - 1 + + +--- + +## Solution for Continuous Subarray Sum Problem + +### Intuition +This is a prefix sum's problem. Due to LC + + A good subarray is a subarray where: + its length is at least two, and + the sum of the elements of the subarray is a multiple of k. + +modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). +For this constraint 1 <= nums.length <= 10^5 an O(n^2) solution may lead to TLE. +For this constraint 1 <= k <= 2^31 - 1, an array version solution might lead to MLE. + +A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k> instead of an array which is accepted by LC. + - Since the computation uses mod_k[prefix].front(), a simple hash table unordered_map mod_k is sufficient for this need. + - An acceptable version using array version when k + + + ```python + + class Solution: + def checkSubarraySum(self, nums: List[int], k: int) -> bool: + n=len(nums) + if n<2: return False + mod_k={} + prefix=0 + mod_k[0]=-1 + for i, x in enumerate(nums): + prefix+=x + prefix%=k + if prefix in mod_k: + if i>mod_k[prefix]+1: + return True + else: + mod_k[prefix]=i + return False + + + + + ``` + + + + + + ```java + + class Solution { + public boolean checkSubarraySum(int[] nums, int k) { + int n = nums.length, prefSum = 0; + Map firstOcc = new HashMap<>(); + firstOcc.put(0, 0); + + for (int i = 0; i < n; i++) { + prefSum = (prefSum + nums[i]) % k; + if (firstOcc.containsKey(prefSum)) { + if (i + 1 - firstOcc.get(prefSum) >= 2) { + return true; + } + } else { + firstOcc.put(prefSum, i + 1); + } + } + + return false; + } +} + + ``` + + + + + ```cpp + + class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + map mp ; + mp[0] = 0 ; + int sum = 0 ; + for(int i = 0 ; i< nums.size() ; i++ ) { + sum += nums[i] ; + int rem = sum%k ; + + if(mp.find(rem)==mp.end()){ + mp[rem] = i+1 ; + + }else if (mp[rem] < i ) { + return true ; + + } + + + } + return false ; + + } +}; + + + ``` + + + + + + + + +## References + +- **LeetCode Problem:** [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) +- **Solution Link:** [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/submissions/1281964300/) +- **Authors GeeksforGeeks Profile:** [parikhit kurmi](https://www.geeksforgeeks.org/user/sololeveler673/) +- **Authors Leetcode:** [parikhit kurmi](https://leetcode.com/u/parikhitkurmi14/) From 30846185b46d25c43e87c5f2dfa07173da773a5d Mon Sep 17 00:00:00 2001 From: Parikhit kurmi <124444610+parikhitritgithub@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:50:14 +0530 Subject: [PATCH 03/11] Update 523. Continuous Subarray Sum.md --- .../0500-0599/523. Continuous Subarray Sum.md | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index 5a4f2323a..0cc4bb575 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -11,6 +11,23 @@ tags: description: "This is a solution to the Continuous Subarray Sum problem on LeetCode." --- +## Problem Description + +Given an integer array `nums` and an integer `k`, return `true` if `nums` has a good subarray or `false` otherwise. A good subarray is a subarray where: + +- Its length is at least two, and +- The sum of the elements of the subarray is a multiple of `k`. + +Note that: +- A subarray is a contiguous part of the array. +- An integer `x` is a multiple of `k` if there exists an integer `n` such that `x = n * k`. 0 is always a multiple of `k`. + +### Examples + +**Example 1:** + + + ## Problem Description Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise. @@ -28,7 +45,6 @@ Note that: ``` - Input: nums = [23,2,4,6,7], k = 6 Output: true Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. @@ -56,10 +72,7 @@ Output: false ### Constraints -- 1 <= nums.length <= 105 -- 0 <= nums[i] <= 109 -- 0 <= sum(nums[i]) <= 231 - 1 -- 1 <= k <= 231 - 1 +- $1 <= nums.length <= 105$ --- @@ -74,7 +87,7 @@ This is a prefix sum's problem. Due to LC the sum of the elements of the subarray is a multiple of k. modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). -For this constraint 1 <= nums.length <= 10^5 an O(n^2) solution may lead to TLE. +For this constraint $1 <= nums.length <= 10^5$ an O(n^2) solution may lead to TLE. For this constraint 1 <= k <= 2^31 - 1, an array version solution might lead to MLE. A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k> instead of an array which is accepted by LC. - Since the computation uses mod_k[prefix].front(), a simple hash table unordered_map mod_k is sufficient for this need. - An acceptable version using array version when k + ```python +//python class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: @@ -117,17 +132,13 @@ Thanks to the comments of @Sergei proposing to use unordered_set, a combination else: mod_k[prefix]=i return False - - - - - ``` - +``` ```java +//java class Solution { public boolean checkSubarraySum(int[] nums, int k) { @@ -150,12 +161,13 @@ Thanks to the comments of @Sergei proposing to use unordered_set, a combination } } - ``` +``` + + + - - - ```cpp +//cpp class Solution { public: @@ -182,8 +194,7 @@ public: } }; - - ``` +``` From 7c0058f9fd27fc0332793c27543f9f0646e6e5eb Mon Sep 17 00:00:00 2001 From: Parikhit kurmi <124444610+parikhitritgithub@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:59:08 +0530 Subject: [PATCH 04/11] Update 523. Continuous Subarray Sum.md --- .../0500-0599/523. Continuous Subarray Sum.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index 0cc4bb575..dd236764e 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -20,7 +20,7 @@ Given an integer array `nums` and an integer `k`, return `true` if `nums` has a Note that: - A subarray is a contiguous part of the array. -- An integer `x` is a multiple of `k` if there exists an integer `n` such that `x = n * k`. 0 is always a multiple of `k`. +- An integer `x` is a multiple of `k` if there exists an integer `n` such that `$x = n * k$`. 0 is always a multiple of `k`. ### Examples @@ -37,7 +37,7 @@ A good subarray is a subarray where: - the sum of the elements of the subarray is a multiple of k. Note that: - A subarray is a contiguous part of the array. - - An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k. + - An integer x is a multiple of k if there exists an integer n such that $x = n * k$. 0 is always a multiple of k. ### Examples @@ -45,7 +45,7 @@ Note that: ``` -Input: nums = [23,2,4,6,7], k = 6 +Input: nums : [23,2,4,6,7], k : 6 Output: true Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. @@ -55,7 +55,7 @@ Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to ``` -Input: root = nums = [23,2,6,4,7], k = 6 +Input: root : nums : [23,2,6,4,7], k : 6 Output: true Explanation: [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. @@ -65,7 +65,7 @@ Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements ``` -Input: nums = [23,2,6,4,7], k = 13 +Input: nums : [23,2,6,4,7], k : 13 Output: false ``` @@ -88,7 +88,7 @@ This is a prefix sum's problem. Due to LC modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). For this constraint $1 <= nums.length <= 10^5$ an O(n^2) solution may lead to TLE. -For this constraint 1 <= k <= 2^31 - 1, an array version solution might lead to MLE. +For this constraint $1 <= k <= 2^31 - 1$, an array version solution might lead to MLE. A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k> instead of an array which is accepted by LC. - Since the computation uses mod_k[prefix].front(), a simple hash table unordered_map mod_k is sufficient for this need. - - An acceptable version using array version when k Date: Mon, 10 Jun 2024 12:07:51 +0530 Subject: [PATCH 05/11] Update 523. Continuous Subarray Sum.md --- .../lc-solutions/0500-0599/523. Continuous Subarray Sum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index dd236764e..d0fdabbb7 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -90,7 +90,7 @@ modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). For this constraint $1 <= nums.length <= 10^5$ an O(n^2) solution may lead to TLE. For this constraint $1 <= k <= 2^31 - 1$, an array version solution might lead to MLE. -A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k Date: Mon, 10 Jun 2024 17:33:42 +0530 Subject: [PATCH 06/11] Update 523. Continuous Subarray Sum.md --- .../0500-0599/523. Continuous Subarray Sum.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index d0fdabbb7..ec475bdce 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -1,7 +1,7 @@ --- -id: Continuous Subarray Sum -title: Continuous Subarray Sum -sidebar_label: 0523 Continuous Subarray Sum +id: continuous subarray sum +title: continuous subarray sum +sidebar_label: 0523 continuous subarray sum tags: - prefix sum + hashmap - LeetCode @@ -72,7 +72,7 @@ Output: false ### Constraints -- $1 <= nums.length <= 105$ +- $1 \leq \text{nums.length} \leq 105$ --- @@ -87,7 +87,7 @@ This is a prefix sum's problem. Due to LC the sum of the elements of the subarray is a multiple of k. modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). -For this constraint $1 <= nums.length <= 10^5$ an O(n^2) solution may lead to TLE. +For this constraint $1 <= nums.length <= 10^5$ an $O(n^2)$ solution may lead to TLE. For this constraint $1 <= k <= 2^31 - 1$, an array version solution might lead to MLE. A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k> instead of an array which is accepted by LC. - - Since the computation uses mod_k[prefix].front(), a simple hash table unordered_map mod_k is sufficient for this need. + - 2nd approach uses `unordered_map>` instead of an array which is accepted by LC. + - Since the computation uses mod_k[prefix].front(), a simple hash table `unordered_map mod_k`is sufficient for this need. - An acceptable version using array version when $k Date: Mon, 10 Jun 2024 17:41:59 +0530 Subject: [PATCH 07/11] Update 523. Continuous Subarray Sum.md --- .../lc-solutions/0500-0599/523. Continuous Subarray Sum.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index ec475bdce..2808002c6 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -1,5 +1,5 @@ --- -id: continuous subarray sum +id: continuous-subarray-sum title: continuous subarray sum sidebar_label: 0523 continuous subarray sum tags: @@ -98,7 +98,7 @@ Thanks to the comments of @Sergei proposing to use unordered_set, a combination ### Approach - - First try uses array version for mod_k(k), but LC's constraints: $1 <= k <= 2^31 - 1$ which leads to MLE. + - First try uses array version for` mod_k(k)`, but LC's constraints: $1 <= k <= 2^31 - 1$ which leads to MLE. - 2nd approach uses `unordered_map>` instead of an array which is accepted by LC. - Since the computation uses mod_k[prefix].front(), a simple hash table `unordered_map mod_k`is sufficient for this need. - An acceptable version using array version when $k Date: Mon, 10 Jun 2024 17:51:10 +0530 Subject: [PATCH 08/11] Update 523. Continuous Subarray Sum.md --- .../0500-0599/523. Continuous Subarray Sum.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index 2808002c6..d7a664227 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -8,7 +8,7 @@ tags: - Java - Python - C++ -description: "This is a solution to the Continuous Subarray Sum problem on LeetCode." +description: This is a solution to the Continuous Subarray Sum problem on LeetCode. --- ## Problem Description @@ -87,8 +87,8 @@ This is a prefix sum's problem. Due to LC the sum of the elements of the subarray is a multiple of k. modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). -For this constraint $1 <= nums.length <= 10^5$ an $O(n^2)$ solution may lead to TLE. -For this constraint $1 <= k <= 2^31 - 1$, an array version solution might lead to MLE. +For this constraint $1 \leq \text{nums.length} \leq 105$ an $O(n^2)$ solution may lead to TLE. + A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k>` instead of an array which is accepted by LC. - - Since the computation uses mod_k[prefix].front(), a simple hash table `unordered_map mod_k`is sufficient for this need. + - Since the computation uses `mod_k[prefix].front()`, a simple hash table `unordered_map mod_k`is sufficient for this need. - An acceptable version using array version when $k Date: Mon, 10 Jun 2024 18:02:08 +0530 Subject: [PATCH 09/11] Update 523. Continuous Subarray Sum.md --- .../0500-0599/523. Continuous Subarray Sum.md | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index d7a664227..643725537 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -8,19 +8,19 @@ tags: - Java - Python - C++ -description: This is a solution to the Continuous Subarray Sum problem on LeetCode. +description: This is a solution to the Continuous Subarray Sum problem on LeetCode --- ## Problem Description -Given an integer array `nums` and an integer `k`, return `true` if `nums` has a good subarray or `false` otherwise. A good subarray is a subarray where: +Given an integer array `nums` and an integer `k`, return `true` if `nums` has a good subarray or `false` otherwise A good subarray is a subarray where -- Its length is at least two, and -- The sum of the elements of the subarray is a multiple of `k`. +- Its length is at least two and +- The sum of the elements of the subarray is a multiple of `k` Note that: -- A subarray is a contiguous part of the array. -- An integer `x` is a multiple of `k` if there exists an integer `n` such that `$x = n * k$`. 0 is always a multiple of `k`. +- A subarray is a contiguous part of the array +- An integer `x` is a multiple of `k` if there exists an integer `n` such that `$x = n * k$` 0 is always a multiple of `k` ### Examples @@ -30,14 +30,14 @@ Note that: ## Problem Description -Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise. +Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise A good subarray is a subarray where: - its length is at least two, and - - the sum of the elements of the subarray is a multiple of k. + - the sum of the elements of the subarray is a multiple of k Note that: - - A subarray is a contiguous part of the array. - - An integer x is a multiple of k if there exists an integer n such that $x = n * k$. 0 is always a multiple of k. + - A subarray is a contiguous part of the array + - An integer x is a multiple of k if there exists an integer n such that $x = n * k$ 0 is always a multiple of k ### Examples @@ -47,7 +47,7 @@ Note that: Input: nums : [23,2,4,6,7], k : 6 Output: true -Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. +Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6 ``` @@ -57,8 +57,8 @@ Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to ``` Input: root : nums : [23,2,6,4,7], k : 6 Output: true -Explanation: [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. +Explanation: [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 ``` **Example 3:** @@ -80,29 +80,29 @@ Output: false ## Solution for Continuous Subarray Sum Problem ### Intuition -This is a prefix sum's problem. Due to LC +This is a prefix sum's problem Due to LC A good subarray is a subarray where: its length is at least two, and - the sum of the elements of the subarray is a multiple of k. + the sum of the elements of the subarray is a multiple of k -modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k). -For this constraint $1 \leq \text{nums.length} \leq 105$ an $O(n^2)$ solution may lead to TLE. +modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k) +For this constraint $1 \leq \text{nums.length} \leq 105$ an $O(n^2)$ solution may lead to TLE -A hash map with care on prefix sum mod k to use is however a tip. The array version is used when k>` instead of an array which is accepted by LC. - - Since the computation uses `mod_k[prefix].front()`, a simple hash table `unordered_map mod_k`is sufficient for this need. - - An acceptable version using array version when $k>` instead of an array which is accepted by LC + - Since the computation uses `mod_k[prefix].front()`, a simple hash table `unordered_map mod_k`is sufficient for this need + - An acceptable version using array version when $k Date: Mon, 10 Jun 2024 18:11:36 +0530 Subject: [PATCH 10/11] Update 523. Continuous Subarray Sum.md --- .../0500-0599/523. Continuous Subarray Sum.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index 643725537..958c70ab1 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -1,6 +1,6 @@ --- id: continuous-subarray-sum -title: continuous subarray sum +title: continuous-subarray-sum sidebar_label: 0523 continuous subarray sum tags: - prefix sum + hashmap @@ -13,7 +13,7 @@ description: This is a solution to the Continuous Subarray Sum problem on LeetCo ## Problem Description -Given an integer array `nums` and an integer `k`, return `true` if `nums` has a good subarray or `false` otherwise A good subarray is a subarray where +Given an integer array `nums` and an integer `k` return `true` if `nums` has a good subarray or `false` otherwise A good subarray is a subarray where - Its length is at least two and - The sum of the elements of the subarray is a multiple of `k` @@ -30,10 +30,10 @@ Note that: ## Problem Description -Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise +Given an integer array nums and an integer k return true if nums has a good subarray or false otherwise A good subarray is a subarray where: - - its length is at least two, and + - its length is at least two and - the sum of the elements of the subarray is a multiple of k Note that: - A subarray is a contiguous part of the array @@ -83,7 +83,7 @@ Output: false This is a prefix sum's problem Due to LC A good subarray is a subarray where: - its length is at least two, and + its length is at least two and the sum of the elements of the subarray is a multiple of k modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k) From aaeb70f58cb9603eeb423127f973486390ad803f Mon Sep 17 00:00:00 2001 From: Parikhit kurmi <124444610+parikhitritgithub@users.noreply.github.com> Date: Mon, 10 Jun 2024 23:17:40 +0530 Subject: [PATCH 11/11] Update 523. Continuous Subarray Sum.md --- .../lc-solutions/0500-0599/523. Continuous Subarray Sum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md index 958c70ab1..988d2a745 100644 --- a/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md +++ b/dsa-solutions/lc-solutions/0500-0599/523. Continuous Subarray Sum.md @@ -90,7 +90,7 @@ modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k) For this constraint $1 \leq \text{nums.length} \leq 105$ an $O(n^2)$ solution may lead to TLE -A hash map with care on prefix sum mod k to use is however a tip The array version is used when k