Smallest subarray which upon repetition gives the original array | Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
def printArray ( brr ) : NEW_LINE INDENT for it in brr : NEW_LINE INDENT print ( it , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT def RepeatingSubarray ( arr , N ) : NEW_LINE INDENT if ( N < 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT brr = [ ] NEW_LINE brr . append ( arr [ 0 ] ) NEW_LINE brr . append ( arr [ 1 ] ) NEW_LINE for i in range ( 2 , N // 2 + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT a = False NEW_LINE n = len ( brr ) NEW_LINE j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT K = j % i NEW_LINE if ( arr [ j ] == brr [ K ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT a = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not a and j == N ) : NEW_LINE INDENT printArray ( brr ) NEW_LINE return NEW_LINE DEDENT DEDENT brr . append ( arr [ i ] ) NEW_LINE DEDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE RepeatingSubarray ( arr , N ) NEW_LINE DEDENT