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
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))
[3,5,1,4,3,1,4,3,1,4,3,9,6,1], 3, 2