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

01 Validate Subsequence

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

// O(n) time
// O(1) space
const twoNumberSum = (arr, sub) => {
  let arrCount = 0
  let subCount = 0
  while (arrCount <= arr.length && subCount < sub.length) {
    if (arr[arrCount] == sub[subCount]) {
      subCount += 1
    }
    arrCount += 1
  }
  console.log(subCount)
  return subCount == sub.length
}

console.log(twoNumberSum([5, 1, 22, 25, 6, -1, 8, 10], [1, 6, -1, 10]))

Created 2021-12-31T12:50:36+00:00 · Edit