6.Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k
Solution I: (Using Prefix Sum)
Time Complexity: O(N^2) , Space Complexity: O(N)
Solution II: (Using Hashmap)
Time Complexity: O(N) , Space Complexity: O(N)
Subarray sum for +ve integer
If a subarray has sum greater than the given sum then there is no possibility that adding elements to the current subarray the sum will be x (given sum). Idea is to use a similar approach to a sliding window. Start with an empty subarray, add elements to the subarray until the sum is less than x. If the sum is greater than x, remove elements from the start of the current subarray.
Last updated