Subarray Sum Equals K - LeetCode 560 - Coding Interview Questions

preview_player
Показать описание

---- ------
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)
Рекомендации по теме
Комментарии
Автор

brilliant explanation of prefix sum, the best of all I've seen on Yourtube. Many thanks man!

USRetro
Автор

Nice this is probably the best explanation

baaaaaaaaaaaaaaaaaaaaaaam
Автор

can i ask question in sliding window in excatly this part :

# Expand sliding window
if total < k:
end += 1
if end == len(nums):
break
total += nums[end]

this snippet of code should be like this:

# Expand sliding window
if total < k:
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: so we need to expand so we increase end pointer by one and if we check first the condition of end == len(nums) will be true and it will break loop and return counter for example = 1 but if we check first when end on element previous last one the condition will be false and we increase end pointer by one and total will change and it will loop again and check if 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

amrabdelatyfathallah
Автор

Thanks for the solution... Love from India

darktez
Автор

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.

novascotia
Автор

Hi bro and thanks. Sliding Window Technique works also for an array with postive values and zeros ?

francescomangano
join shbcf.ru