BackmediumArraysSwiggy

Consecutive Subsequence Validator Solution

Problem Statement

Given a list of integers asteroidSizes and a target list targetSequence, determine if the targetSequence appears in the same order and is consecutive within the asteroidSizes list.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
true

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we can see that the target sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] appears in the same order and is consecutive in the asteroidSizes list. Therefore, the output is true.

Example 2
Input
[1, 2, 3, 2, 3, 4, 2, 3, 4, 5]
Output
false

Explanation: Step-by-step: Given the input [1, 2, 3, 2, 3, 4, 2, 3, 4, 5], we can see that the target sequence [1, 2, 3, 2, 3, 4, 2, 3, 4, 5] does not appear in the same order and is not consecutive in the asteroidSizes list. Therefore, the output is false.

Constraints

  • 1 <= asteroidSizes.length <= 10^5
  • 1 <= targetSequence.length <= 100
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Consecutive Subsequence Validator — Problem Statement & Solution Guide

ArraysMediumLinear Scan
TimeO(n)
|
SpaceO(1)

Problem Description

Given a list of integers asteroidSizes and a target list targetSequence, determine if the targetSequence appears in the same order and is consecutive within the asteroidSizes list.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Output

true

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we can see that the target sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] appears in the same order and is consecutive in the asteroidSizes list. Therefore, the output is true.

Example 2

Input

[1, 2, 3, 2, 3, 4, 2, 3, 4, 5]

Output

false

Explanation: Step-by-step: Given the input [1, 2, 3, 2, 3, 4, 2, 3, 4, 5], we can see that the target sequence [1, 2, 3, 2, 3, 4, 2, 3, 4, 5] does not appear in the same order and is not consecutive in the asteroidSizes list. Therefore, the output is false.

Constraints

  • 1 <= asteroidSizes.length <= 10^5
  • 1 <= targetSequence.length <= 100

Optimal Approach & Strategy

The optimal approach involves using a single pass through the asteroidSizes array and iterating over the targetSequence array to verify consecutive matches, resulting in a time complexity of O(n). This approach ensures efficiency even for large inputs. It utilizes a two-pointer technique to track progress.

Brute Force Approach

A brute-force approach would involve iterating over the asteroidSizes array and checking every possible subsequence to see if it matches the targetSequence, resulting in a time complexity of O(n²). This approach would be inefficient for large inputs. It would also require additional nested loops to verify consecutive matches.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function consecutiveSubsequenceValidator(asteroidSizes, targetSequence) {
  let n = asteroidSizes.length;
  let m = targetSequence.length;
  for (let i = 0; i <= n - m; i++) {
    let isValid = true;
    for (let j = 0; j < m; j++) {
      if (asteroidSizes[i + j] !== targetSequence[j]) {
        isValid = false;
        break;
      }
    }
    if (isValid) return true;
  }
  return false;
}

Asked in Top Tech Interviews

Swiggy

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.