← Questions

Count subarrays that match a comparison pattern

0
DatabricksCodingLast asked Sep 2024

A pattern of length m holds 1 (the next value goes up), 0 (it stays equal) and -1 (it goes down). Count the subarrays of length m + 1 whose consecutive comparisons follow the pattern exactly. Turn the array into its sequence of comparisons first; the follow-up is doing it in linear time with KMP or a Z-function when the arrays are large.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding