Detect Pattern of Length M Repeated K or More Times in an Array
In this lab, you'll be building a function to detect a pattern of length m
within an array that is repeated k
or more times. You'll be given an array of positive integers, and your task is to write a function called containsPattern
which takes three parameters – arr
, m
, and k
. The function must return true
if there exists a pattern of length m
that is repeated k
or more times, otherwise, it should return false
.
A pattern is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times consecutively without overlapping. A pattern is defined by its length and the number of repetitions.
Examples
Here are a few examples of how the function should behave:
Example 1:
containsPattern([1, 2, 4, 4, 4, 4], 1, 3)
Output: true
Explanation: The pattern (4) of length 1 is repeated 4 consecutive times. Notice that the pattern can be repeated k or more times but not less.
Example 2:
containsPattern([1, 2, 1, 2, 1, 1, 1, 3], 2, 2)
Output: true
Explanation: The pattern (1, 2) of length 2 is repeated 2 consecutive times. Another valid pattern (2, 1) is also repeated 2 times.
Example 3:
containsPattern([1, 2, 1, 2, 1, 3], 2, 3)
Output: false
Explanation: The pattern (1, 2) is of length 2 but is repeated only 2 times. There is no pattern of length 2 that is repeated 3 or more times.
Constraints
To complete this lab, make sure your implementation fulfills the following constraints:
2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
Make sure to export your function so that it can be used in the evaluation script.