Maximize difference between odd and even indexed array elements by shift operations | Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver Code
def minimize ( n ) : NEW_LINE INDENT optEle = n NEW_LINE strEle = str ( n ) NEW_LINE for idx in range ( len ( strEle ) ) : NEW_LINE INDENT temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE optEle = min ( optEle , temp ) NEW_LINE DEDENT return optEle NEW_LINE DEDENT def maximize ( n ) : NEW_LINE INDENT optEle = n NEW_LINE strEle = str ( n ) NEW_LINE for idx in range ( len ( strEle ) ) : NEW_LINE INDENT temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE optEle = max ( optEle , temp ) NEW_LINE DEDENT return optEle NEW_LINE DEDENT def minimumDifference ( arr ) : NEW_LINE INDENT caseOne = 0 NEW_LINE minVal = 0 NEW_LINE maxVal = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE minVal += minimize ( arr [ i ] ) NEW_LINE else : NEW_LINE maxVal += maximize ( arr [ i ] ) NEW_LINE DEDENT caseOne = abs ( maxVal - minVal ) NEW_LINE caseTwo = 0 NEW_LINE minVal = 0 NEW_LINE maxVal = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE maxVal += maximize ( arr [ i ] ) NEW_LINE else : NEW_LINE minVal += minimize ( arr [ i ] ) NEW_LINE DEDENT caseTwo = abs ( maxVal - minVal ) NEW_LINE print ( max ( caseOne , caseTwo ) ) NEW_LINE DEDENT arr = [ 332 , 421 , 215 , 584 , 232 ] NEW_LINE minimumDifference ( arr ) NEW_LINE