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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimize ( int n ) { int optEle = n ; string strEle = to_string ( n ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { int temp = stoi ( strEle . substr ( idx ) + strEle . substr ( 0 , idx ) ) ; optEle = min ( optEle , temp ) ; } return optEle ; } int maximize ( int n ) { int optEle = n ; string strEle = to_string ( n ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { int temp = stoi ( strEle . substr ( idx ) + strEle . substr ( 0 , idx ) ) ; optEle = max ( optEle , temp ) ; } return optEle ; } void minimumDifference ( int arr [ ] , int N ) { int caseOne = 0 ; int minVal = 0 ; int maxVal = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = abs ( maxVal - minVal ) ; int caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = abs ( maxVal - minVal ) ; } cout << max ( caseOne , caseTwo ) << endl ; } int main ( ) { int arr [ ] = { 332 , 421 , 215 , 584 , 232 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumDifference ( arr , N ) ; return 0 ; }