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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
string checkPattern(int arr[], int m,
int k, int n)
{
int count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (int i = 0; i < n - m; i++) {
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m]) {
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m) {
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k) {
return "Yes";
}
}
}
else {
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
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
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
static String checkPattern(int arr[], int m,
int k, int n)
{
int count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (int i = 0; i < n - m; i++)
{
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m])
{
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m)
{
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k)
{
return "Yes";
}
}
}
else
{
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
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));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
# Function to check if array can be split
# into K consecutive and non-overlapping
# subarrays of length M consisting of a
# single distinct element
def checkPattern(arr, m, k, n):
count = 1
t = 0
# Traverse over the range [0, N - M - 1]
for i in range(n - m):
# Check if arr[i] is the
# same as arr[i + m]
if (arr[i] == arr[i + m]):
# Increment current length
# t of pattern matched by 1
t += 1
# Check if t is equal to m,
# increment count of total
# repeated pattern
if (t == m):
t = 0
count += 1
# Return true if length of
# total repeated pattern is k
if (count == k):
return "Yes"
else:
# Update length of the
# current pattern
t = 0
# Update count to 1
count = 1
# Finally return false if
# no pattern found
return "No"
# Driver Code
if __name__ == '__main__':
arr = [6, 1, 3, 3, 3, 3]
M = 1
K = 3
N = len(arr)
print(checkPattern(arr, M, K, N))
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
static String checkPattern(int []arr, int m,
int k, int n)
{
int count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (int i = 0; i < n - m; i++)
{
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m])
{
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m)
{
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k)
{
return "Yes";
}
}
}
else
{
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
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));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Js program for the above approach
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
function checkPattern( arr, m, k, n)
{
let count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (let i = 0; i < n - m; i++) {
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m]) {
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m) {
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k) {
return "Yes";
}
}
}
else {
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
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)
Similar Reads
Check if an array can be split into subsets of K consecutive elements Given an array arr[] and integer K, the task is to split the array into subsets of size K, such that each subset consists of K consecutive elements. Examples: Input: arr[] = {1, 2, 3, 6, 2, 3, 4, 7, 8}, K = 3 Output: true Explanation: The given array of length 9 can be split into 3 subsets {1, 2, 3}
5 min read
Maximum sum of K-length subarray consisting of same number of distinct elements as the given array Given an array arr[] consisting of N integers and an integer K, the task is to find a subarray of size K with maximum sum and count of distinct elements same as that of the original array. Examples: Input: arr[] = {7, 7, 2, 4, 2, 7, 4, 6, 6, 6}, K = 6Output: 31Explanation: The given array consists o
15+ min read
Check if any subarray of length M repeats at least K times consecutively or not 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
12 min read
Count ways to generate an array having distinct elements at M consecutive indices Given an array arr[] consisting of N integers in the range [0, M] and an integer M, the task is to count the number of ways to replace all array elements whose value is 0 with non-zero values from the range [0, M] such that all possible M consecutive elements are distinct. Examples: Input: arr[] = {
10 min read
Check if an array can be split into K non-overlapping subarrays whose Bitwise AND values are equal Given an array arr[] of size N and a positive integer K, the task is to check if the array can be split into K non-overlapping and non-empty subarrays such that Bitwise AND of all the subarrays are equal. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: arr[] = { 3, 2,
11 min read