Maximize K to make given array Palindrome when each element is replaced by its remainder with K | utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; variable to store the largest K that makes A palindromic ; return the required answer ; Driver code ; Input ; Function call
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def largestK ( A , N ) : NEW_LINE INDENT l , r , flag = 0 , N - 1 , 0 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( A [ l ] != A [ r ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT K = abs ( A [ 0 ] - A [ N - 1 ] ) NEW_LINE for i in range ( 1 , N // 2 ) : NEW_LINE INDENT K = gcd ( K , abs ( A [ i ] - A [ N - i - 1 ] ) ) NEW_LINE DEDENT return K NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( largestK ( A , N ) ) NEW_LINE DEDENT