Notice that most of the subarrays overlap in some way. In [3, 2, 5, 1]
, [3, 2]
is part of [3, 2, 5]
. It seems there's an opportunity to keep track of just one thing for now.
The final solution has us using a hash map/dict to store previous subarray sums.


xxxxxxxxxx
39
var assert = require('assert');
function subarraySum(nums, n) {
// hash of prefix sums
// Fill in this method
return nums;
}
const arr = [1, 2, 3],
sum = 5;
console.log(subarraySum(arr, sum));
try {
assert.equal(subarraySum([1, 2, 3], 3), true);
console.log('PASSED: ' + 'assert.equal(subarraySum([ 1, 2, 3 ], 3), true)');
} catch (err) {
console.log(err);
}
try {
assert.equal(subarraySum([], 3), false);
console.log('PASSED: ' + 'assert.equal(subarraySum([], 3), false)');
} catch (err) {
console.log(err);
OUTPUT
Results will appear here.