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> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!