![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check pattern of length m repeated K or more times exists or not in Python
Suppose we have an array nums with positive values, we have to find a pattern of length m that is repeated k or more than k times. Here a pattern is a non-overlapping subarray (consecutive) that consists of one or more values and are repeated multiple times. A pattern is defined by its length and number of repetitions. We have to check whether there exists a pattern of length m that is repeated k or more times or not.
So, if the input is like nums = [3,5,1,4,3,1,4,3,1,4,3,9,6,1], m = 3, k = 2, then the output will be True because there is a pattern [1,4,3] which is present 3 times.
To solve this, we will follow these steps −
for i in range 0 to size of nums - 1, do
sub1 := sub array of nums from index i to (i+m*k) - 1
sub2 := k consecutive sub array of nums from index i to (i+m-1)
if sub1 is same as sub2, then
return True
return False
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums, m, k): for i in range(len(nums)): sub1 = nums[i:i+m*k] sub2 = nums[i:i+m]*k if sub1 == sub2: return True return False nums = [3,5,1,4,3,1,4,3,1,4,3,9,6,1] m = 3 k = 2 print(solve(nums, m, k))
Input
[3,5,1,4,3,1,4,3,1,4,3,9,6,1], 3, 2
Output
True