code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

03 Subarray Product Less Than K

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const subarrayProductLessThanK = (nums, k) => {
  if (k <= 1) return 0
  let multi = 1
  let result = 0
  let left = 0
  for (let right = 0; right < nums.length; right++) {
    multi *= nums[right]
    while (multi >= k && left < nums.length) {
      multi = multi / nums[left]
      left += 1
    }
    result = result + right - left + 1
  }
  return result
}

console.log(subarrayProductLessThanK([10, 5, 2, 6], 100))

Created 2022-02-09T01:17:13+00:00 · Edit