0

So I'm working on this problem:

Given an array with positive numbers and a target number, find all of its contiguous subarrays whose product is less than the target number.

Input: find_subarrays([2,5,3,10], 30)

Output: [2][5][2,5][3][5,3][10]

I have two arrays:

  1. a temporary one, tmp, that is reset whenever the product of the contiguous numbers is greater than the target

  2. a result, that holds all the contiguous arrays and is returned at the end

I originally tried using tmp.push(arr[j]) to add numbers into the tmp array and then push the tmp array into the result array.

But the result is [2,5][2,5][5,3][5,3][3][10]. I don't understand what is going on.

When I use tmp = tmp.concat(arr[j]), I get the correct output.

Why doesn't tmp.push(arr[j]) work in this situation?

My code:

const find_subarrays = function(arr, target) {
  let result = [];
  for (let i=0; i<arr.length; i++) {
    let product = arr[i];
    let tmp = [];
    let j = i;
    while (product < target && j < arr.length) {
      tmp = tmp.concat(arr[j]);
      // tmp.push(arr[j]); why doesn't this work instead of concat? 
      result.push(tmp)
      j++;
      product *= arr[j]
    }
  }
  return result;
};
Will
  • 39
  • 7
  • 2
    Does this answer your question? [Difference between concat and push?](https://stackoverflow.com/questions/44572026/difference-between-concat-and-push) – Philip Jan 07 '20 at 21:30
  • With `tmp.push` you don't create a new array, but mutate `tmp`, which you had already pushed to the result in a previous iteration of the inner loop. `concat` however creates a new array -- the previously pushed `tmp` is not affected by that operation. – trincot Jan 07 '20 at 21:33
  • 1
    Dupe target doesn't answer the asker's question and I can no longer submit my answer. @Will the problem is that `tmp.push(arr[j]))` *mutates* the array held in `tmp`, and every time you call `result.push(tmp)` in the while loop, it pushes a reference to that same array which is getting mutated. When you use `tmp = tmp.concat(arr[j]);` it assigns `tmp` to a *new* array, and thus every time you push to the results it pushes a different array. – Klaycon Jan 07 '20 at 21:33
  • Have you tried debugging by, let's say, outputting the array when using `push()` or `concat()`? – rolory Jan 07 '20 at 21:39
  • After the first loop in the while loop, tmp = [2] and result = [2]. If I use the push method, after the second loop, tmp = [2, 5] and result = [2, 5], [2, 5]. Can you explain what is going on here? When I push 5 into tmp, I'm mutating tmp and tmp becomes [2, 5], correct? Then when I push tmp, which is [2, 5] into result on this second loop, shouldn't I get [2, [2, 5]]? I'm not understanding how the mutation is affecting this. – Will Jan 07 '20 at 22:32
  • For my own benefit, I'm answering my question. At the first loop, tmp = [2] and result = [tmp], which so happens to be [2] because tmp = [2]. At the second loop, tmp = [2, 5] and result = [tmp, tmp], which at this point is [[2, 5], [2, 5]], because tmp = [2, 5]. – Will Jan 07 '20 at 22:49

0 Answers0