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
def check ( arr , M , K , ind ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( 1 , K , 1 ) : NEW_LINE INDENT if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def SubarrayRepeatsKorMore ( arr , N , M , K ) : NEW_LINE INDENT for ind in range ( N - M * K + 1 ) : NEW_LINE INDENT if ( check ( arr , M , K , ind ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ] NEW_LINE M = 2 NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT