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
function minimize ( n ) { let optEle = n ; let strEle = ( n ) . toString ( ) ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let temp = parseInt ( strEle . substring ( idx ) + strEle . substring ( 0 , idx ) ) ; optEle = Math . min ( optEle , temp ) ; } return optEle ; } function maximize ( n ) { let optEle = n ; let strEle = n . toString ( ) ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let temp = parseInt ( strEle . substring ( idx ) + strEle . substring ( 0 , idx ) ) ; optEle = Math . max ( optEle , temp ) ; } return optEle ; } function minimumDifference ( arr ) { let caseOne = 0 ; let minVal = 0 ; let maxVal = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = Math . abs ( maxVal - minVal ) ; let caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = Math . abs ( maxVal - minVal ) ; } document . write ( Math . max ( caseOne , caseTwo ) + " " ) ; } let arr = [ 332 , 421 , 215 , 584 , 232 ] ; minimumDifference ( arr ) ;