Subarray Sum Equals K - LeetCode 560 - Coding Interview Questions
Link to the Problem : leetcode.com/problems/subarra...
Buy Me a Coffee ☺️ : www.buymeacoffee.com/Onecodeman
---- ------
The problem is that they give us an array of integers, and they ask us to return the total number of subarrays whose sum equals to K.
To Solve this Problem we gonna use two techniques Sliding Window and prefix-sum.
Next, we present the solution and walk you through the code step by step, highlighting the key points and explaining the logic behind each part of the solution.
Whether you are preparing for a technical interview or just looking to improve your coding skills, this video is a great resource.
If you found this video helpful, like, subscribe, comment.
#codinginterviewquestions #leetcodedailychallenge #leetcodesolution #leetcodedailychallenge #leetcodequestionandanswers
---- ----
Understanding the problem : (0:00)
How do you ask questions in coding interview? : (0:38)
Sliding Window Technique : (1:19)
Complexity Analysis : (4:04)
Code Explanation : (4:16)
Prefix-sum Technique : (5:46)
Complexity Analysis : (9:32)
Code Explanation : (9:45)
Пікірлер: 11
Nice this is probably the best explanation
Thanks for the solution... Love from India
@onecodeman
Жыл бұрын
Thanks Brother for the Support 🙏
can i ask question in sliding window in excatly this part : # Expand sliding window if total end += 1 if end == len(nums): break total += nums[end] this snippet of code should be like this: # Expand sliding window if total if end == len(nums): break end += 1 total += nums[end] because imagine if we have start pointer anywhere in array and end on element previous last one and total k or total == k or total < k lets say it will be total becomes equal to k it will increase counter by one and shrink window and it will break the loop after this and correct for me if iam wrong
If the input array can contain negative numbers, we need to modify the sliding window approach to handle negative sums. One way to handle negative numbers is to use a hash table to store the number of occurrences of each prefix sum encountered so far. At each iteration, we can check if the difference between the current prefix sum and the target sum k is already in the hash table. If it is, we add the number of occurrences of that prefix sum to the answer.
@onecodeman
Жыл бұрын
Amazing solution. Alternatively, you can use the third solution (prefix sum), which is more efficient.
Hi bro and thanks. Sliding Window Technique works also for an array with postive values and zeros ?
@msakshayakumar
8 ай бұрын
doesn't seem like it because we move the start index once total == sum. maybe if we change the logic to: if total greater than K, then move the start index, we could make it work?
love you
@onecodeman
Жыл бұрын
🙏