Advertisement
Guest User

Untitled

a guest
Oct 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Runtime: 72 ms, faster than 56.55% of JavaScript online submissions for Continuous Subarray Sum.
  3. Memory Usage: 36.5 MB, less than 100.00% of JavaScript online submissions for Continuous Subarray Sum.
  4.  */
  5. const checkSubarraySum = (nums, k) => {
  6.     if (nums.length < 2) {
  7.         return false
  8.     }
  9.     if (k === 1) {
  10.         return true
  11.     }
  12.     for (let i = 0; i < nums.length; i++) {
  13.         let sum = 0
  14.         for (let j = i; j < nums.length; j++) {
  15.             sum += nums[j]
  16.             if (j - i < 1) {
  17.                 continue
  18.             }
  19.             if (i === 0) {
  20.                 if (sum === 0 && k === 0 || hasRepeatedZero(nums, i, j)) {
  21.                     return true  
  22.                 }
  23.             }
  24.             if (sum % k === 0) {
  25.                 return true
  26.             }
  27.         }
  28.         if (i === 0 && sum < k) {
  29.             return false
  30.         }
  31.     }
  32.     return false
  33. };
  34.  
  35. function hasRepeatedZero(nums, i, j) {
  36.     return nums[j] === 0 && nums[j - 1] === 0
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement