Problem · Array

Subarray Counting

Learn this problem
EasyDatabricks logoDatabricksINTERNOA

Problem statement

pattern describes adjacent comparisons: 1 means the next value is greater, 0 means equal, and -1 means smaller.

Return the number of contiguous subarrays of numbers whose pattern.length adjacent comparisons exactly equal pattern. Each candidate subarray therefore has length pattern.length + 1.

Function

subarrayCounting(numbers: int[], pattern: int[]) → int

Examples

Example 1

numbers = [4, 1, 3, 4, 4, 5, 5, 1]pattern = [1, 0, -1]return = 1

Only [4,5,5,1] matches: it increases, stays equal, and then decreases.

Constraints

🍇🍇

More Databricks problems

drafts saved locally
public int subarrayCounting(int[] numbers, int[] pattern) {
  // write your code here
}
numbers[4, 1, 3, 4, 4, 5, 5, 1]
pattern[1, 0, -1]
expected1
checking account