This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given an array of integers and an integer **k**, you need to find the total number of continuous subarrays whose sum equals to **k**.

**Example 1:**

Input:nums = [1,1,1], k = 2Output:2

**Note:**

- The length of the array is in range [1, 20,000].
- The range of numbers in the array is [-1000, 1000] and the range of the integer
**k**is [-1e7, 1e7].

b'

\n## Summary

\n## Solution

\n

\n#### Approach #1 Brute Force [Time Limit Exceeded]

\n

\n#### Approach #2 Using Cummulative sum [Accepted]

\n

\n#### Approach #3 Without space [Accepted]

\n

\n#### Approach #4 Using hashmap [Accepted]

\n

\n

'
\n\n

\n\n

**Algorithm**

The simplest method is to consider every possible subarray of the given array, find the sum of the elements of each of those subarrays and check for the equality of the sum obtained with the given . Whenver the sum equals , we can increment the used to store the required result.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Considering every possible subarray takes time. For each of the subarray we calculate the sum taking time in the worst case, taking a total of time.

\n \n - \n
Space complexity : . Constant space is used.

\n \n

\n

**Algorithm**

Instead of determining the sum of elements everytime for every new subarray considered, we can make use of a cumulative sum array , . Then, in order to calculate the sum of elements lying between two indices, we can subtract the cumulative sum corresponding to the two indices to obtain the sum directly, instead of iterating over the subarray to obtain the sum.

\nIn this implementation, we make use of a cumulative sum array, , such that is used to store the cumulative sum of array upto the element corresponding to the index. Thus, to determine the sum of elements for the subarray , we can directly use .

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Considering every possible subarray takes time. Finding out the sum of any subarray takes time after the initial processing of for creating the cumulative sum array.

\n \n - \n
Space complexity : . Cumulative sum array of size is used.

\n \n

\n

**Algorithm**

Instead of considering all the and points and then finding the sum for each subarray corresponding to those points, we can directly find the sum on the go while considering different points. i.e. We can choose a particular point and while iterating over the points, we can add the element corresponding to the point to the sum formed till now. Whenver the equals the required value, we can update the value. We do so while iterating over all the indices possible for every index. Whenver, we update the index, we need to reset the value to 0.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . We need to consider every subarray possible.

\n \n - \n
Space complexity : . Constant space is used.

\n \n

\n

**Algorithm**

The idea behind this approach is as follows: If the cumulative sum(repreesnted by for sum upto index) upto two indices is the same, the sum of the elements lying in between those indices is zero. Extending the same thought further, if the cumulative sum upto two indices, say and is at a difference of i.e. if , the sum of elements lying between indices and is .

\nBased on these thoughts, we make use of a hashmap which is used to store the cumulative sum upto all the indices possible along with the number of times the same sum occurs. We store the data in the form: . We traverse over the array and keep on finding the cumulative sum. Every time we encounter a new sum, we make a new entry in the hashmap corresponding to that sum. If the same sum occurs again, we increment the count corresponding to that sum in the hashmap. Further, for every sum encountered, we also determine the number of times the sum has occured already, since it will determine the number of times a subarray with sum has occured upto the current index. We increment the by the same amount.

\nAfter the complete array has been traversed, the gives the required result.

\nThe animation below depicts the process.

\n!?!../Documents/560_Subarray.json:1000,563!?!

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . The entire array is traversed only once.

\n \n - \n
Space complexity : . Hashmap can contain upto distinct entries in the worst case.

\n \n

\n

Analysis written by: @vinod23

\n