-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Continuous Subarray sum #113
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some general comments
- It's great that we have three different approaches, however, the thing I find missing is a good transition between the three. For e.g. when we start with approach 2, it's always good to point out why we even need an approach 2. You could say that the previous approach was taking cubic time complexity and also timed out on the online judge. Then go on to explain what we change/improve in the previous approach thus giving rise to the new approach. This gives a smooth transition between approaches.
- Similarly, add a paragraph for the transition from approach 2 to approach 3.
- Also, it would be great to have C++ solutions for approaches 2 and 3 as well considering you added it for the first solution.
- Finally, I would recommend adding some basic comments to all the code segments. Even though things may be obvious, it is still advisable to add comments. It's always a good practice to have them in sync with the algorithm itself.
--- | ||
|
||
### 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wWe have to find a continuo
|
||
### 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<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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While traversing the subsets we explicitily explicitly to need handle the case
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change the heading to Naive Way (TLE)
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to mention the complexity analysis for each solution there and then rather than in the end for all three solutions. Also, a one-line explanation can be added in my opinion for each of the complexity sections just to add a bit of clarity even if they are obvious enough
|
||
|
||
|
||
### Solution-2 : Dynamic Programming |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not exactly a dynamic programming solution. We are just doing some preprocessing to avoid repeated computation. Any DP solution has two main properties:
- It can be broken down into sub-problems and
- We can make use of optimal solutions for sub-problems for solving the main problem.
This solution doesn't fit the bill as far as these conditions are concerned. It would be better to name it as Accumulated sum pre-processing
or something. At the end of the day, we still check each and every subset for the condition and I don't think there is a recursive element to this problem.
|
||
``` | ||
|
||
<p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd have to change the image to change the DP array to something like a preprocessed array or something.
#### 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 `i<j` and `j-i>1`. | ||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention the complexity analysis right after this section as is mentioned in the template.
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to have an Algorithm section like in the previous two approaches to explain this algorithm in more detail. Also, if possible, add a figure here to showcase the algorithm. That would be awesome to have!
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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change the name dp
to something else
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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change the name dp
to something else
Continuous Subarray Sum