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.
The naive solution involves traversing all the subset of nums whose length>=2 and checking for nums[i:j]%K==0 where i<j and nums[i:j]= nums[i]+nums[i+1]+...+nums[j-1]+nums[j]. While traversing the subsets we explicitily need handle the case K=0 because nums[i:j]%K will throw DivideByZeroException.
- Create all the subsets of
numsarray wholelength>=2. - Traverse the subsets and check if their sum is a multiple of
K - While traversing the subsets check whether
K=0which requires sum of the elements in the subset be equal to zero.
This solution will throw Time Limit Exceeded error when run on leet code.
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]
- Create a array called
dpwheredp[i] = nums[0:i] - We will check each subset for the condition
dp[j]-dp[i]=N*Kwherei<jandj-i>1. - 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.
Let us assume any two index positions(i,j) and 0<i<j<len(nums) in dp array where
dp[i] = p*K ie dp[i]%K=0
dp[j] = q*K ie dp[j]%K=0
Let us take a difference of the above two
dp[j] - dp[i] = (q-p)*K
which transforms to
dp[j] - dp[i] = N*K where N is any integer
From the above we can conclude that, while traversing the array if we store dp[i]%K in a hashmap and check if dp[j]%K already exists in hashmap and check if j-i>1 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.
Solution-1
- Time Complexity:
O(N^3)whereNis the length of the input. - Space Complexity:
O(1)
Solution-2
- Time Complexity:
O(N^2)whereNis the length of the input. - Space Complexity:
O(N)whereNis the length of the input. This space is occupied bydp
Solution-3
- Time Complexity:
O(N)whereNis the length of the input. - Space Complexity:
O(N+k)whereNis the length of the input andkis size of the hashmap . The space is occupied bydpandhashmap
https://leetcode.com/problems/continuous-subarray-sum/
Article contributed by Arihant Sai


