Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 1001 <= arr[i] <= 1001 <= m <= 1002 <= k <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,4,4,4,4] 1 3
[1,2,1,2,1,1,1,3] 2 2
[1,2,1,2,1,3] 2 3
maximum-repeating-substring)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1566: Detect Pattern of Length M Repeated K or More Times
class Solution {
public boolean containsPattern(int[] arr, int m, int k) {
if (arr.length < m * k) {
return false;
}
int cnt = 0, target = (k - 1) * m;
for (int i = m; i < arr.length; ++i) {
if (arr[i] == arr[i - m]) {
if (++cnt == target) {
return true;
}
} else {
cnt = 0;
}
}
return false;
}
}
// Accepted solution for LeetCode #1566: Detect Pattern of Length M Repeated K or More Times
func containsPattern(arr []int, m int, k int) bool {
cnt, target := 0, (k-1)*m
for i := m; i < len(arr); i++ {
if arr[i] == arr[i-m] {
cnt++
if cnt == target {
return true
}
} else {
cnt = 0
}
}
return false
}
# Accepted solution for LeetCode #1566: Detect Pattern of Length M Repeated K or More Times
class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
if len(arr) < m * k:
return False
cnt, target = 0, (k - 1) * m
for i in range(m, len(arr)):
if arr[i] == arr[i - m]:
cnt += 1
if cnt == target:
return True
else:
cnt = 0
return False
// Accepted solution for LeetCode #1566: Detect Pattern of Length M Repeated K or More Times
struct Solution;
impl Solution {
fn contains_pattern(arr: Vec<i32>, m: i32, k: i32) -> bool {
let m = m as usize;
let k = k as usize;
arr.windows(m * k)
.any(|w| w.chunks(m).all(|v| *v == w[0..m]))
}
}
#[test]
fn test() {
let arr = vec![1, 2, 4, 4, 4, 4];
let m = 1;
let k = 3;
let res = true;
assert_eq!(Solution::contains_pattern(arr, m, k), res);
let arr = vec![1, 2, 1, 2, 1, 1, 1, 3];
let m = 2;
let k = 2;
let res = true;
assert_eq!(Solution::contains_pattern(arr, m, k), res);
let arr = vec![1, 2, 1, 2, 1, 3];
let m = 2;
let k = 3;
let res = false;
assert_eq!(Solution::contains_pattern(arr, m, k), res);
let arr = vec![1, 2, 3, 1, 2];
let m = 2;
let k = 2;
let res = false;
assert_eq!(Solution::contains_pattern(arr, m, k), res);
let arr = vec![2, 2, 2, 2];
let m = 2;
let k = 3;
let res = false;
assert_eq!(Solution::contains_pattern(arr, m, k), res);
let arr = vec![2, 2];
let m = 1;
let k = 2;
let res = true;
assert_eq!(Solution::contains_pattern(arr, m, k), res);
}
// Accepted solution for LeetCode #1566: Detect Pattern of Length M Repeated K or More Times
function containsPattern(arr: number[], m: number, k: number): boolean {
if (arr.length < m * k) {
return false;
}
const target = (k - 1) * m;
let cnt = 0;
for (let i = m; i < arr.length; ++i) {
if (arr[i] === arr[i - m]) {
if (++cnt === target) {
return true;
}
} else {
cnt = 0;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.