Check if an array can be split into K consecutive non | Java 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 ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code
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 ) ) ; } }