Check if any subarray of length M repeats at least K times consecutively or not
Last Updated :
11 Jul, 2022
Given an array arr[] consisting of N integers and two positive integers M and K, the task is to check if there exists any subarray of length M that repeats consecutively at least K times. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {2, 1, 2, 1, 1, 1, 3}, M = 2, K = 2
Output: Yes
Explanation: The subarray {2, 1} of length 2 repeats at least K(= 2) times consecutively.
Input: arr[] = {7, 1, 3, 1, 1, 1, 1, 3}, M = 1, K = 3
Output: Yes
Naive Approach: The simplest approach is to generate all possible subarrays of length M and check for each subarray, whether on concatenating it exactly K times is present as a subarray in the given array or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool check( int arr[], int M, int K,
int ind)
{
for ( int i = 0; i < M; i++) {
for ( int j = 1; j < K; j++) {
if (arr[ind + i]
!= arr[ind + i + j * M]) {
return false ;
}
}
}
return true ;
}
bool SubarrayRepeatsKorMore(
int arr[], int N, int M, int K)
{
for ( int ind = 0;
ind <= N - M * K; ind++) {
if (check(arr, M, K, ind)) {
return true ;
}
}
return false ;
}
int main()
{
int arr[] = { 2, 1, 2, 1, 1, 1, 3 };
int M = 2, K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
if (SubarrayRepeatsKorMore(
arr, N, M, K)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static boolean check( int arr[], int M,
int K, int ind)
{
for ( int i = 0 ; i < M; i++)
{
for ( int j = 1 ; j < K; j++)
{
if (arr[ind + i] != arr[ind + i + j * M])
{
return false ;
}
}
}
return true ;
}
static boolean SubarrayRepeatsKorMore( int arr[], int N,
int M, int K)
{
for ( int ind = 0 ; ind <= N - M * K; ind++)
{
if (check(arr, M, K, ind))
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 };
int M = 2 , K = 2 ;
int N = arr.length;
if (SubarrayRepeatsKorMore(arr, N, M, K))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def check(arr, M, K, ind):
for i in range (M):
for j in range ( 1 , K, 1 ):
if (arr[ind + i] ! = arr[ind + i + j * M]):
return False
return True
def SubarrayRepeatsKorMore(arr, N, M, K):
for ind in range (N - M * K + 1 ):
if (check(arr, M, K, ind)):
return True
return False
if __name__ = = '__main__' :
arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ]
M = 2
K = 2
N = len (arr)
if (SubarrayRepeatsKorMore(arr, N, M, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool check( int [] arr, int M, int K,
int ind)
{
for ( int i = 0; i < M; i++)
{
for ( int j = 1; j < K; j++)
{
if (arr[ind + i] != arr[ind + i + j * M])
{
return false ;
}
}
}
return true ;
}
static bool SubarrayRepeatsKorMore( int [] arr, int N,
int M, int K)
{
for ( int ind = 0; ind <= N - M * K; ind++)
{
if (check(arr, M, K, ind))
{
return true ;
}
}
return false ;
}
static void Main()
{
int [] arr = { 2, 1, 2, 1, 1, 1, 3 };
int M = 2, K = 2;
int N = arr.Length;
if (SubarrayRepeatsKorMore(
arr, N, M, K))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function check(arr, M, K, ind)
{
for (let i = 0; i < M; i++)
{
for (let j = 1; j < K; j++)
{
if (arr[ind + i] !=
arr[ind + i + j * M])
{
return false ;
}
}
}
return true ;
}
function SubarrayRepeatsKorMore(arr, N, M, K)
{
for (let ind = 0;
ind <= N - M * K; ind++)
{
if (check(arr, M, K, ind))
{
return true ;
}
}
return false ;
}
let arr = [ 2, 1, 2, 1, 1, 1, 3 ];
let M = 2, K = 2;
let N = arr.length;
if (SubarrayRepeatsKorMore(arr, N, M, K))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(N*M*K)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using Two Pointers Technique. Follow the steps below to solve the problem:
- Initialize a variable, say count as 0.
- Traverse the given array arr[] over the range of indices [0, N – M] using a variable, say i, and perform the following steps:
- If the value of arr[i] is equal to arr[i + M], then increment count by 1, as there is a match in the subarray.
- Otherwise, update count to 0 as there is a break in the contiguous subarrays.
- If the value of count is M * (K – 1), then it means that there are K consecutively equal subarrays of length M. Therefore, print “Yes” and break out of the loop.
- After completing the above steps, if the count never becomes M * (K – 1), then print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool checkExists( int arr[], int N,
int M, int K)
{
int count = 0;
for ( int i = 0; i < N - M; i++) {
if (arr[i] == arr[i + M])
count++;
else
count = 0;
if (count == M * (K - 1))
return true ;
}
return false ;
}
int main()
{
int arr[] = { 2, 1, 2, 1, 1, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = 2, K = 2;
if (checkExists(arr, N, M, K)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static boolean checkExists( int arr[], int N,
int M, int K)
{
int count = 0 ;
for ( int i = 0 ; i < N - M; i++)
{
if (arr[i] == arr[i + M])
count++;
else
count = 0 ;
if (count == M * (K - 1 ))
return true ;
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 };
int M = 2 , K = 2 ;
int N = arr.length;
if (checkExists(arr, N, M, K))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def checkExists(arr, N, M, K):
count = 0
for i in range (N - M):
if (arr[i] = = arr[i + M]):
count + = 1
else :
count = 0
if (count = = M * (K - 1 )):
return True
return False
if __name__ = = '__main__' :
arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ]
N = len (arr)
M = 2
K = 2
if (checkExists(arr, N, M, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool checkExists( int []arr, int N,
int M, int K)
{
int count = 0;
for ( int i = 0; i < N - M; i++)
{
if (arr[i] == arr[i + M])
count++;
else
count = 0;
if (count == M * (K - 1))
return true ;
}
return false ;
}
public static void Main()
{
int []arr = { 2, 1, 2, 1, 1, 1, 3 };
int N = arr.Length;
int M = 2, K = 2;
if (checkExists(arr, N, M, K))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function checkExists(arr, N, M, K)
{
let count = 0;
for (let i = 0; i < N - M; i++) {
if (arr[i] == arr[i + M])
count++;
else
count = 0;
if (count == M * (K - 1))
return true ;
}
return false ;
}
let arr = [ 2, 1, 2, 1, 1, 1, 3 ];
let N = arr.length;
let M = 2, K = 2;
if (checkExists(arr, N, M, K)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...