Zero Sum Subarray + find indices of sub array

  • N, N

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?

Last updated