Detect Pattern of Length M Repeated K or More Times - Problem

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.

Input & Output

Example 1 — Basic Pattern Found
$ Input: arr = [1,2,4,4,4,4], m = 1, k = 3
Output: true
💡 Note: Pattern [4] of length 1 repeats 4 times starting at index 2, which is ≥ 3 repetitions required
Example 2 — Pattern Too Short
$ Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
Output: true
💡 Note: Pattern [1,2] of length 2 repeats exactly 2 times starting at index 0: [1,2,1,2]
Example 3 — No Pattern Found
$ Input: arr = [1,2,3,1,2], m = 2, k = 3
Output: false
💡 Note: No pattern of length 2 repeats 3 or more times. Array length 5 < 2×3=6 required

Constraints

  • 2 ≤ arr.length ≤ 100
  • 1 ≤ arr[i] ≤ 100
  • 1 ≤ m ≤ 100
  • 2 ≤ k ≤ 100

Visualization

Tap to expand
Detect Pattern of Length M Repeated K or More Times INPUT Array arr: 1 i=0 2 i=1 4 i=2 4 i=3 4 i=4 4 i=5 Pattern "4" repeats 4 times Parameters m = 1 (pattern length) k = 3 (min repetitions) Convert to string matching: "1,2,4,4,4,4" Find "4" repeated 3+ times ALGORITHM STEPS 1 Convert Array to String Join elements with delimiter 2 Build Pattern String Create m-length pattern x k 3 Sliding Window Check Compare consecutive elements 4 Count Matches Track consecutive equal pairs Pattern Matching Process arr[i] == arr[i+m] ? i=2: arr[2]=4 == arr[3]=4 OK i=3: arr[3]=4 == arr[4]=4 OK i=4: arr[4]=4 == arr[5]=4 OK Count = 4 >= k(3) FOUND! FINAL RESULT Pattern Found in Array: 1 2 4 4 4 4 4 consecutive "4"s (k=3 needed, found 4) Output: true Verification Pattern: [4] (length m=1) Repetitions found: 4 Required (k): 3 4 >= 3 : Condition Met! Key Insight: String Pattern Matching converts the array problem into substring search. For each position i, we check if arr[i] == arr[i+m] for (k-1)*m consecutive positions. This ensures the pattern of length m repeats k times. Time Complexity: O(n*m*k), Space: O(1). TutorialsPoint - Detect Pattern of Length M Repeated K or More Times | String Pattern Matching Approach
Asked in
Google 25 Facebook 20
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen