code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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