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 –
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
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.
Here are a few examples of how the function should behave:
containsPattern([1, 2, 4, 4, 4, 4], 1, 3)
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.
containsPattern([1, 2, 1, 2, 1, 1, 1, 3], 2, 2)
Explanation: The pattern (1, 2) of length 2 is repeated 2 consecutive times. Another valid pattern (2, 1) is also repeated 2 times.
containsPattern([1, 2, 1, 2, 1, 3], 2, 3)
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.
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.