Description
Solutions
Subarray Counting
🤘 INTERN

note - feel free to see the Problem Source section below for the original problem description and example explanation ;)

Imagine you have a list of numbers and a smaller list called pattern, which describes how the numbers should change. The pattern only contains three types of instructions:

  • 1 means the next number should be larger than the previous one.
  • 0 means the next number should be equal to the previous one.
  • -1 means the next number should be smaller than the previous one.
  • Your job is to find how many stretches of consecutive numbers in the big list follow this exact pattern. Just make sure the solution isn't too slow—though it doesn’t need to be perfect, it should work efficiently enough.

    Example 1:

    Input:  numbers = [4, 1, 3, 4, 4, 5, 5, 1], pattern = [1, 0, -1]
    Output: 1
    Explanation:
    Let’s explore some possible subarrays of length 3. We don't need to check the subarray starting at numbers[0]—there's no number before the first element to compare! Subarray [1, 3, 4] doesn’t match. The pattern starts with 1, meaning the first number should be larger than the one before it, but numbers[1] = 1 is less than numbers[0] = 4. Subarray [3, 4, 4] fails because the pattern says 0 for the second number, meaning it should stay the same, but numbers[3] = 4 is greater than numbers[2] = 3. Subarray [4, 4, 5] doesn’t work either. The pattern ends with -1, meaning the last number should be smaller than the previous one, but numbers[5] = 5 is greater than numbers[4] = 4. Subarray [4, 5, 5] also doesn’t match for the same reason. Finally, subarray [5, 5, 1] perfectly matches the pattern: numbers[5] > numbers[4], as the pattern starts with 1. numbers[6] = numbers[5], following the 0 in the pattern. numbers[7] < numbers[6], just as the pattern ends with -1. This subarray is the match we’re looking for!
    Constraints:
      🍇🍇
    Thumbnail 0
    Thumbnail 1
    Testcase

    Result
    Case 1

    input:

    output: