- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- C# Program to check whether a directory exists or not
- Java Program to check whether a file exists or not
- Program to check regular expression pattern is matching with string or not in Python
- How to check if a file exists or not using Python?
- Program to check n can be shown as sum of k or not in Python
- C++ Program to find if the given string has Repeated Subsequence of Length 2 or More
- Program to check whether all palindromic substrings are of odd length or not in Python
- Check if a word exists in a grid or not in Python
- Program to check n can be represented as sum of k primes or not in Python
- Program to check if array pairs are divisible by k or not using Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether odd length cycle is in a graph or not in Python
- How to check if a file exists or not in Java?
- Program to check whether first player can take more candies than other or not in Python
- Program to check sum of two numbers is up to k from sorted List or not in Python
