JavaScript: return all contiguous subarrays whose sum equals K

Joji

This is a variant of this leetcode question, but instead of returning the count, we want to return the actual contiguous sub arrays. For example, if num = [1,2,4,7] k=7 the returned value should be [[1,2,4],[7]] .

I used a hashmap to store the cumulative sum up to all the indices possible along with the number of times the same sum occurs for the original question, where it asks to return the count

var subarraySum = function(nums, k) {
  let sum = 0;
  let count = 0;
  const myMap = new Map();
  myMap.set(0, 1);

  for (let num of nums) {
    sum += num;
    count += myMap.get(sum - k) || 0;
    myMap.set(sum, (myMap.get(sum) || 0) + 1);
  }
  
  return count;
}

But I cannot seem to figure out how I can adapt this solution to return the actual sub-arrays.

Shridhar R Kulkarni

Below is an efficient solution with a minor tweak to the code your are referring to. This solution iterates over the input array once + whatever is required to add a subarray to the solution.

On the below line, you know the count if increases, you have something to add to your solution.

count += myMap.get(sum - k) || 0;

Now we need to understand at what index does sum - k exists in the map. On the below line, you are counting only the number of times the sum occurs.

myMap.set(sum, (myMap.get(sum) || 0) + 1);

Instead of just the count, you need to store the index* at which the sum occurs. Get rid of the counts throughout the code and focus only on index.

The pseudo code now looks like below:

for(int i = 0; i < nums.size(); i++):
    sum += nums[i]
    if(sum-k exists in the map):
        start_index = get the index corresponding to that sum-k
        //end_index will be the i
        add to the result set the subarray from start_index to i (or end_index)
    Set in map the sum and index i appropriately

*I hope the resultant subarrays don't overlap. Otherwise, store a list of indices instead of an index.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Finding number of subarrays whose sum equals `k`

Find the maximum sum of all contiguous subarrays and not consecutive each others which its length at most 'k' in a given array

Cumulative sum to find Subarrays' whose sum equals a give value

Is it possible to get the sum of all contiguous subarrays in O(n) time?

Sum of products of elements of all subarrays of length k

Algorithm that generates all contiguous subarrays

Split an array into equal contiguous subarrays of equal sum

Sum of contiguous subarrays of an array in least time

Maximize the minimum subarray sum out of all subarrays when an array is divided into K subarrays/groups

Return all subsets whose sum is a given value (subset sum problem)

Count number of subarrays whose product is not divisible by k

How to return objects that are repeated in all subarrays from the main array in Javascript

Determine whether any permutation of a given array such that the sum of all subarrays of length K are equal

Maximum value of sum of K partitioned subarrays

largest sum of contiguous subarray No Larger than k

Find the number of subarrays whose average is greater than or equal K

How to find the number of contiguous subsequences / subarrays whose product can be expressed as difference of squares of 2 random integers?

How to find the number of subarrays in a given array whose sum is zero

XOR on contiguous subarrays of an array

Counting contiguous sawtooth subarrays

Recursively generate all k-digit numbers whose digit-sum is n

Minimum size of subarray whose sum is k

Finding k elements in array whose product equals given number

Maximum sum of contiguous sub-sequence with length at most k

Find the kth smallest number whose sum equals m

Python classes: find arrays whose sum equals a specific number

Find a pair of elements from an array whose sum equals a given number

How to generate 15 random numbers whose sum equals a specific number

Sum of two integers in an array equals K