Minimum count of elements required to obtain the given Array by repeated mirror operations | Function to find minimum number of elements required to form A [ ] by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the final answer ; Driver code
def minimumrequired ( A , N ) : NEW_LINE INDENT K = N NEW_LINE while ( K > 0 ) : NEW_LINE INDENT if ( K % 2 ) == 1 : NEW_LINE INDENT ans = K NEW_LINE break NEW_LINE DEDENT ispalindrome = 1 NEW_LINE for i in range ( 0 , K // 2 ) : NEW_LINE INDENT if ( A [ i ] != A [ K - 1 - i ] ) : NEW_LINE INDENT ispalindrome = 0 NEW_LINE DEDENT DEDENT if ( ispalindrome == 1 ) : NEW_LINE INDENT ans = K // 2 NEW_LINE K = K // 2 NEW_LINE DEDENT else : NEW_LINE INDENT ans = K NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumrequired ( A , N ) ) NEW_LINE