Maximize difference between odd and even indexed array elements by shift operations | C # program for the above approach ; 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
using System ; class GFG { static int minimize ( int n ) { int optEle = n ; string strEle = n . ToString ( ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { int temp = Int32 . Parse ( strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ) ; optEle = Math . Min ( optEle , temp ) ; } return optEle ; } static int maximize ( int n ) { int optEle = n ; string strEle = n . ToString ( ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { int temp = Int32 . Parse ( strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ) ; optEle = Math . Max ( optEle , temp ) ; } return optEle ; } static void minimumDifference ( int [ ] arr ) { int caseOne = 0 ; int minVal = 0 ; int maxVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = Math . Abs ( maxVal - minVal ) ; int caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = Math . Abs ( maxVal - minVal ) ; } Console . WriteLine ( Math . Max ( caseOne , caseTwo ) ) ; } public static void Main ( ) { int [ ] arr = { 332 , 421 , 215 , 584 , 232 } ; minimumDifference ( arr ) ; } }