human
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"
How can the above be solved in Python?
assistant
#Here is code to maximize K to make given array Palindrome when each element is replaced by its remainder with K and the other steps required.
 def gcd ( a , b ) :
	if ( b == 0 ) :
		return a
	else :
		return gcd ( b , a % b )
def largestK ( A , N ) :
	l , r , flag = 0 , N - 1 , 0
	while ( l < r ) :
		if ( A [ l ] != A [ r ] ) :
			flag = 1
			break
		l += 1
		r -= 1
	if ( flag == 0 ) :
		return - 1
	K = abs ( A [ 0 ] - A [ N - 1 ] )
	for i in range ( 1 , N // 2 ) :
		K = gcd ( K , abs ( A [ i ] - A [ N - i - 1 ] ) )
	return K
if __name__ == ' _ _ main _ _ ' :
	A = [ 1 , 2 , 3 , 2 , 1 ]
	N = len ( A )
	print ( largestK ( A , N ) )