Check if any subarray of length M repeats at least K times consecutively or not | Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver Code
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 ( " " ) ; } else { document . write ( " " ) ; }