MetaOA #3
Subarray Matches a Comparison Pattern
Estimated MediumJudge-ready
Topics (AI-labelled)In-place ArraysStrings
Given an integer array a and a pattern p of length m. The values of p are 1, 0, and −1, requiring the next element to be greater than, equal to, or less than the previous one, respectively. Count the contiguous subarrays of length exactly m+1 whose adjacent comparisons all match the pattern; overlaps are allowed.
Input
The first line contains n m (2≤n≤10000, 1≤m<n); the second line contains n integers (−10⁹..10⁹); the third line contains m pattern values.
Output
Output the answer as described; separate multiple results with spaces in order, and output an empty line for an empty array.
Example 1
Input:
8 3
1 4 4 1 3 5 5 3
1 0 -1
Output:
2
Example 2
Input:
4 2
2 2 2 2
0 0
Output:
2
Example 3
Input:
3 1
3 2 1
1
Output:
0
Example notes
Example 1: [1,4,4,1] starting at 0 and [3,5,5,3] starting at 4 both go up, stay equal, then go down in order; no other window matches, so there are 2 in total.
Editorial
Loading solution…