diff --git a/Dynamic-Programming/ContinuousSubarraySum/README.md b/Dynamic-Programming/ContinuousSubarraySum/README.md new file mode 100644 index 0000000..82f2c01 --- /dev/null +++ b/Dynamic-Programming/ContinuousSubarraySum/README.md @@ -0,0 +1,112 @@ +## Continous Subarray Sum + +

+Question Screenshot +

+ +--- + +### Motivation +We are given an array of Integers `nums` and `K`. we have to find a continuous subset of the array of `length>=2` whose sum is of the form `N*K` where `N` is any integer. + + +### Solution-1 : Naive Way + +The naive solution involves traversing all the subset of `nums` whose `length>=2` and checking for `nums[i:j]%K==0` where `i=2`. +2. Traverse the subsets and check if their sum is a multiple of `K` +3. While traversing the subsets check whether `K=0` which requires sum of the elements in the subset be equal to zero. + +

+Naive Algorithm +

+ +This solution will throw `Time Limit Exceeded` error when run on leet code. + + + +### Solution-2 : Dynamic Programming +In the previous solution we were repeatedly calculating the sum of the subsets. Here, we try to preprocess the array so that we don't have to calculate the sum everytime. Let us see how we accomplish this. + +Let's say + +``` +sum[0:0] = nums[0] +sum[0:1] = nums[0] + nums[1] +sum[0:2] = nums[0] + nums[1] + nums[2] +sum[0:3] = nums[0] + nums[1] + nums[2] + nums[3] +``` +from above, we can conclude that + +``` +sum[2:3] = nums[2]+nums[3] = sum[0:3] - sum[0:1] +``` + +If the above procedure is implemented, it will save us from repetitive recomputation of sum of subsets for which we already calculated. + +We create a temporary array called `dp` same length as of input where `dp[i]=sum[0:i]` + +``` +dp[i] = nums[0] + nums[1] +...+ nums[i] + +``` + +

+dp +

+ +#### Algorithm +1. Create a array called `dp` where `dp[i] = nums[0:i]` +2. We will check each subset for the condition `dp[j]-dp[i]=N*K` where `i1`. +3. The above step is similar to the one we used in Solution-1 except this time we wont need to recompute the sum for each subset generated. + +### Solution-3 : Hashmap +Let us assume any two index positions(i,j) and `01` then we have a continous subarray sum. + +The handling of the case `K=0` is same as the previous methods. This is a serious improvement in time complexity from the solution-2. + + +#### Complexity Analysis + +Solution-1 + +* Time Complexity: `O(N^3)` where `N` is the length of the input. +* Space Complexity: `O(1)` + +Solution-2 + +* Time Complexity: `O(N^2)` where `N` is the length of the input. +* Space Complexity: `O(N)` where `N` is the length of the input. This space is occupied by `dp` + +Solution-3 + +* Time Complexity: `O(N)` where `N` is the length of the input. +* Space Complexity: `O(N+k)` where `N` is the length of the input and `k` is size of the hashmap . The space is occupied by `dp` and `hashmap` + +#### Link to OJ +https://leetcode.com/problems/continuous-subarray-sum/ + +--- +Article contributed by [Arihant Sai](https://github.com/Arihant1467) + diff --git a/Dynamic-Programming/ContinuousSubarraySum/solution-1.cpp b/Dynamic-Programming/ContinuousSubarraySum/solution-1.cpp new file mode 100644 index 0000000..a6db9e9 --- /dev/null +++ b/Dynamic-Programming/ContinuousSubarraySum/solution-1.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + + int n=nums.size(); + for(int i=0;i bool: + + for i in range(len(nums)-1): + for j in range(i+1,len(nums)): + summation = sum(nums[i:j+1]) + if k==0: + if summation==0: + return True + elif summation%k==0: + return True + return False \ No newline at end of file diff --git a/Dynamic-Programming/ContinuousSubarraySum/solution-2.py b/Dynamic-Programming/ContinuousSubarraySum/solution-2.py new file mode 100644 index 0000000..d0cb014 --- /dev/null +++ b/Dynamic-Programming/ContinuousSubarraySum/solution-2.py @@ -0,0 +1,29 @@ +class Solution(object): + def checkSubarraySum(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: bool + """ + if len(nums)<2: + return False + + dp = [0]*len(nums) + dp[0]=nums[0] + + for i in range(1,len(nums)): + dp[i]=dp[i-1]+nums[i] + + if k==0 and dp[i]==0: + return True + elif k and dp[i]%k==0: + return True + + for i in range(0,len(nums)-2): + for j in range(i+2,len(nums)): + if dp[j]-dp[i]==0 and k==0: + return True + elif k and (dp[j]-dp[i])%k==0: + return True + + return False \ No newline at end of file diff --git a/Dynamic-Programming/ContinuousSubarraySum/solution-3.py b/Dynamic-Programming/ContinuousSubarraySum/solution-3.py new file mode 100644 index 0000000..61ad4ef --- /dev/null +++ b/Dynamic-Programming/ContinuousSubarraySum/solution-3.py @@ -0,0 +1,34 @@ +class Solution(object): + def checkSubarraySum(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: bool + """ + + dp = [0]*len(nums) + result = {} + dp[0] = nums[0] + for i in range(1,len(nums)): + dp[i] = dp[i-1]+nums[i] + + if len(nums)<2: + return False + + if k==0: + for i in range(1,len(nums)): + if nums[i]==0 and nums[i-1]==0: + return True + return False + + for i in range(len(dp)): + if not i==0 and dp[i]%k==0: + return True + else: + if not (dp[i]%k in result.keys()): + result[dp[i]%k]=i + else: + if i-result[dp[i]%k]>1: + return True + + return False \ No newline at end of file diff --git a/Images/ContinuousSubarraySum/ContinuousSubArraySum-NaiveAlgo.png b/Images/ContinuousSubarraySum/ContinuousSubArraySum-NaiveAlgo.png new file mode 100644 index 0000000..83c841c Binary files /dev/null and b/Images/ContinuousSubarraySum/ContinuousSubArraySum-NaiveAlgo.png differ diff --git a/Images/ContinuousSubarraySum/dp-creation.png b/Images/ContinuousSubarraySum/dp-creation.png new file mode 100644 index 0000000..5398074 Binary files /dev/null and b/Images/ContinuousSubarraySum/dp-creation.png differ diff --git a/Images/ContinuousSubarraySum/question.png b/Images/ContinuousSubarraySum/question.png new file mode 100644 index 0000000..d88adf8 Binary files /dev/null and b/Images/ContinuousSubarraySum/question.png differ diff --git a/README.md b/README.md index 5de1364..683cac4 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,12 @@ There are multiple ways in which you can contribute. There are no "prerequisites LeetCode Link + + 9 + Continuous Subarray Sum + LeetCode + Link +

Graphs and Trees