Skip to main content
Back to problems
#1566
Easy Algorithms

Detect pattern of length m repeated k or more times

Array Enumeration
43.8% acceptance
Feb 25, 2026
689
147
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn contains_pattern(arr: Vec<i32>, m: i32, k: i32) -> bool {
    let m = m as usize;
    let k = k as usize;
    let n = arr.len();
    if m * k > n {
      return false;
    }
    for i in 0..=(n - m * k) {
      let mut ok = true;
      'outer: for rep in 1..k {
        for j in 0..m {
          if arr[i + j] != arr[i + rep * m + j] {
            ok = false;
            break 'outer;
          }
        }
      }
      if ok {
        return true;
      }
    }
    false
  }
}