# Zero Sum Subarray + find indices of sub array

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-37c04795a6137a52518a46b7dcd7e4b627e54f7d%2FScreenshot%202023-01-21%20at%2021.15.30.png?alt=media)

* N, N

```jsx
function zeroSumSubarray(nums) {

  let set = new Set([0]);
  
  let currentSum = 0;
  for (let i = 0; i < nums.length; i++) {
    currentSum += nums[i];
    if (set.has(currentSum)) return true;
    set.add(currentSum);
  }
  
  return false;
}

// Do not edit the line below.
exports.zeroSumSubarray = zeroSumSubarray;
```

* Can I find index?

```jsx
function zeroSumSubarray(nums) {

  const answer = [];
  let set = new Set([0]);
  let sums = [];

  let idxToStop = -1;
  let currentSum = 0;
  for (let i = 0; i < nums.length; i++) {
    currentSum += nums[i];
    sums[i] = currentSum;
    if (set.has(currentSum)) {
      idxToStop = i;
      answer.push(idxToStop);
      break;
    }
    set.add(currentSum);
  }

  if (idxToStop === -1) return [];

  for (let i = idxToStop - 1; i >= 0; --i) {
    if (sums[i] === currentSum) break;
    answer.push(i);
  }
  
  
  return answer;
}

// Do not edit the line below.
exports.zeroSumSubarray = zeroSumSubarray;
```
