Given an array of positive integers arr
, find a pattern of length m
that is repeated k
or more times.
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.
Return true
if there exists a pattern of length m
that is repeated k
or more times, otherwise return false
.
Example 1:
Input: arr = [1,2,4,4,4,4], m = 1, k = 3 Output: true Explanation: The pattern (4) of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.
Example 2:
Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 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:
Input: arr = [1,2,1,2,1,3], m = 2, k = 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:
2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach is like trying every single possible option to see if it works. We want to find a repeating pattern within a larger sequence, so we'll check every potential pattern and see if it repeats enough times.
Here's how the algorithm would work step-by-step:
def detect_pattern_brute_force(sequence, pattern_length, repetition_count):
sequence_length = len(sequence)
for i in range(1, pattern_length + 1):
# Trying different potential pattern lengths
for starting_index in range(sequence_length - i * repetition_count + 1):
potential_pattern = sequence[starting_index:starting_index + i]
repetitions = 1
current_index = starting_index + i
# Need to verify consecutive repetitions of pattern
while current_index + i <= sequence_length and sequence[current_index:current_index + i] == potential_pattern:
repetitions += 1
current_index += i
# Check if the pattern repeats enough times
if repetitions >= repetition_count:
return True
return False
To efficiently find repeating patterns, we slide a window of the specified pattern length across the sequence. We count how many times this window repeats consecutively, stopping early if we don't find enough repetitions.
Here's how the algorithm would work step-by-step:
def detect_pattern(arr, pattern_length, repetition_count):
array_length = len(arr)
for start_index in range(array_length - pattern_length * repetition_count + 1):
number_of_repeats = 0
# Check for consecutive repetitions
while start_index + (number_of_repeats + 1) * pattern_length <= array_length:
is_match = True
for index_in_pattern in range(pattern_length):
if arr[start_index + index_in_pattern] != \
arr[start_index + (number_of_repeats + 1) * pattern_length + index_in_pattern]:
is_match = False
break
if is_match:
number_of_repeats += 1
else:
break
# Return true if we found enough repetitions
if number_of_repeats + 1 >= repetition_count:
return True
return False
Case | How to Handle |
---|---|
Empty input array (arr is null or empty) | Return false immediately as no pattern can exist in an empty array. |
m is zero or negative | Return false, as a pattern length cannot be zero or negative. |
k is zero or negative | Return false as repetition count must be a positive integer. |
m is larger than the array length | Return false, as a pattern of length m cannot fit within arr. |
k * m is larger than the array length | Return false, as the total length of the repeated pattern exceeds the array length. |
Array contains all identical values | The algorithm should correctly detect the repeated pattern if m and k allow for it. |
Integer overflow when calculating indices or lengths, especially with large arrays or values of m or k | Use appropriate data types (e.g., long in Java/C++) to prevent integer overflow issues. |
Input array contains negative numbers | The algorithm must work correctly with negative numbers in the array. |