{x}
blog image

Detect Pattern of Length M Repeated K or More Times

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

Solution Explanation: Detecting Repeated Patterns

The problem asks to determine if a given array contains a pattern of length m that repeats k or more times consecutively. The solution employs a single-pass traversal for efficiency.

Approach:

  1. Early Exit: If the array's length is less than m * k, a pattern of length m repeating k times is impossible. The function immediately returns false.

  2. Sliding Window and Count: The core logic iterates through the array starting from index m. It compares each element arr[i] with the element m positions before it, arr[i - m].

  3. Consecutive Match Count: If a match is found (arr[i] == arr[i - m]), a counter cnt is incremented. This counter tracks the number of consecutive matches of the potential pattern.

  4. Pattern Detection: The target number of consecutive matches is (k - 1) * m. If cnt reaches this value, it means the pattern of length m has repeated k times consecutively, so the function returns true.

  5. Resetting the Count: If a mismatch occurs (arr[i] != arr[i - m]), the pattern is broken, and cnt is reset to 0.

  6. No Pattern Found: If the loop completes without finding k consecutive repetitions of the pattern, the function returns false.

Time Complexity: O(n), where n is the length of the input array. The algorithm performs a single pass through the array.

Space Complexity: O(1). The algorithm uses a constant amount of extra space to store the counter cnt and other variables.

Code Examples (with detailed comments):

Python:

class Solution:
    def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
        n = len(arr)
        
        # Early exit if a pattern of length m repeating k times is impossible.
        if n < m * k:
            return False
        
        cnt = 0  # Counter for consecutive matches
        target = (k - 1) * m  # Target number of consecutive matches for k repetitions
 
        for i in range(m, n):
            if arr[i] == arr[i - m]: # Check for match with element m positions before
                cnt += 1
                if cnt == target:  # Pattern found
                    return True
            else:
                cnt = 0  # Reset counter upon mismatch
 
        return False  # No pattern found

Java:

class Solution {
    public boolean containsPattern(int[] arr, int m, int k) {
        int n = arr.length;
        if (n < m * k) return false; // Early exit
 
        int cnt = 0; // Counter for consecutive matches
        int target = (k - 1) * m; // Target count for k repetitions
 
        for (int i = m; i < n; ++i) {
            if (arr[i] == arr[i - m]) { // Match found
                cnt++;
                if (cnt == target) return true; // Pattern found
            } else {
                cnt = 0; // Reset counter on mismatch
            }
        }
        return false; // No pattern found
    }
}

The other languages (C++, Go, TypeScript) follow a very similar structure, differing only in syntax. The core logic remains the same efficient single-pass approach.