Check if an array can be split into K consecutive non-overlapping subarrays of length M consisting of single distinct element
Last Updated :
21 May, 2021
Given two integers M and K and an array arr[] consisting of N positive integers, the task is to check if the array can be split into K consecutive non-overlapping subarrays of length M such that each subarray consists of a single distinct element. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {6, 1, 3, 3, 3, 3}, M = 1, K = 3
Output: Yes
Explanation:
The K consecutive non-overlapping subarrays are {6}, {1}, {3, 3, 3, 3}.
Input: arr[] = {3, 5, 3, 5, 3, 1}, M = 2, K = 3
Output: No
Approach: The given problem can be solved by using a simple array traversal and checking whether the element at the current index i and the element at the index (i + M) are the same or not. Follow the steps below to solve the problem:
- Initialize two variables, count and t with 1 and 0 respectively, to store the total count of pattern matched and the current length of pattern matched, respectively.
- Traverse the given array over the range [0, N – M – 1] using the variable i and do the following:
- If the value of arr[i] and arr[i + M] is the same, then, increment t by 1 and if t is the same as m then update t to 0 and increment the count. If the value of count is K then print “Yes” and break out of the loop.
- Else, if t is M then increase the count by 1.
- After the above steps, if the value of count is not the same as K then print “No” as there doesn’t exist any such pattern.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string checkPattern( int arr[], int m,
int k, int n)
{
int count = 1, t = 0;
for ( int i = 0; i < n - m; i++) {
if (arr[i] == arr[i + m]) {
t++;
if (t == m) {
t = 0;
count++;
if (count == k) {
return "Yes" ;
}
}
}
else {
t = 0;
count = 1;
}
}
return "No" ;
}
int main()
{
int arr[] = { 6, 1, 3, 3, 3, 3 };
int M = 1, K = 3;
int N = sizeof (arr) / sizeof (arr[0]);
cout << checkPattern(arr, M, K, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String checkPattern( int arr[], int m,
int k, int n)
{
int count = 1 , t = 0 ;
for ( int i = 0 ; i < n - m; i++)
{
if (arr[i] == arr[i + m])
{
t++;
if (t == m)
{
t = 0 ;
count++;
if (count == k)
{
return "Yes" ;
}
}
}
else
{
t = 0 ;
count = 1 ;
}
}
return "No" ;
}
public static void main(String[] args)
{
int arr[] = { 6 , 1 , 3 , 3 , 3 , 3 };
int M = 1 , K = 3 ;
int N = arr.length;
System.out.print(checkPattern(arr, M, K, N));
}
}
|
Python3
def checkPattern(arr, m, k, n):
count = 1
t = 0
for i in range (n - m):
if (arr[i] = = arr[i + m]):
t + = 1
if (t = = m):
t = 0
count + = 1
if (count = = k):
return "Yes"
else :
t = 0
count = 1
return "No"
if __name__ = = '__main__' :
arr = [ 6 , 1 , 3 , 3 , 3 , 3 ]
M = 1
K = 3
N = len (arr)
print (checkPattern(arr, M, K, N))
|
C#
using System;
public class GFG
{
static String checkPattern( int []arr, int m,
int k, int n)
{
int count = 1, t = 0;
for ( int i = 0; i < n - m; i++)
{
if (arr[i] == arr[i + m])
{
t++;
if (t == m)
{
t = 0;
count++;
if (count == k)
{
return "Yes" ;
}
}
}
else
{
t = 0;
count = 1;
}
}
return "No" ;
}
public static void Main(String[] args)
{
int []arr = { 6, 1, 3, 3, 3, 3 };
int M = 1, K = 3;
int N = arr.Length;
Console.Write(checkPattern(arr, M, K, N));
}
}
|
Javascript
<script>
function checkPattern( arr, m, k, n)
{
let count = 1, t = 0;
for (let i = 0; i < n - m; i++) {
if (arr[i] == arr[i + m]) {
t++;
if (t == m) {
t = 0;
count++;
if (count == k) {
return "Yes" ;
}
}
}
else {
t = 0;
count = 1;
}
}
return "No" ;
}
let arr = [ 6, 1, 3, 3, 3, 3 ];
let M = 1, K = 3;
let N = arr.length;
document.write( checkPattern(arr, M, K, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...