Maximize difference between odd and even indexed array elements by shift operations
Last Updated :
23 Jun, 2021
Given an array arr[] of size N, the task is to maximize the absolute difference between the sum of even indexed elements and the sum of odd indexed elements by left shift or right shift of array elements any number of times.
Examples:
Input: arr[] = {332, 421, 215, 584, 232}
Output: 658
Explanation:
Convert the array to {233, 421, 152, 845, 223},
Sum of odd indexed elements = 608.
Sum of even indexed elements = 1266.
Therefore, the difference equals 658.
Input: arr[] = {11, 22, 33, 44, 55}
Output: 33
Approach: The idea is to minimize the value of any one of even or odd indexed array elements and maximize that of the other in order to maximize their absolute difference. Follow the steps below to solve the problem:
- Two possible cases exists. Either to minimize the even indexed array elements and maximize the odd indexed array elements or to minimize the odd indexed array elements and maximize the even indexed array elements.
- For minimizing an element, apply all the shift operations and take the minimum possible value. Similarly, to maximize an element, apply all the shift operations and take the maximum possible value.
- Take the maximum difference from both the cases.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to minimize array
// elements by shift operations
int minimize(int n)
{
int optEle = n;
string strEle = to_string(n);
// For checking all the
// left shift operations
for (int idx = 0; idx < strEle.length();idx++)
{
// Left shift
int temp = stoi(strEle.substr(idx) +
strEle.substr(0, idx));
// Consider the minimum possible value
optEle = min(optEle, temp);
}
return optEle;
}
// Function to maximize array
// elements by shift operations
int maximize(int n)
{
int optEle = n;
string strEle = to_string(n);
// For checking all the
// left shift operations
for (int idx = 0; idx < strEle.length();idx++)
{
// Left shift
int temp = stoi(strEle.substr(idx) +
strEle.substr(0, idx));
// Consider the maximum possible value
optEle = max(optEle, temp);
}
return optEle;
}
// Function to maximize the absolute
// difference between even and odd
// indexed array elements
void minimumDifference(int arr[], int N)
{
int caseOne = 0;
int minVal = 0;
int maxVal = 0;
// To calculate the difference of
// odd indexed elements
// and even indexed elements
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;
// To calculate the difference
// between odd and even indexed
// array elements
for (int i = 0; i < N; i++)
{
if (i % 2 == 0)
maxVal += maximize(arr[i]);
else
minVal += minimize(arr[i]);
caseTwo = abs(maxVal - minVal);
}
// Print the maximum value
cout << max(caseOne, caseTwo) << endl;
}
// Driver code
int main()
{
int arr[] = { 332, 421, 215, 584, 232 };
int N = sizeof(arr) / sizeof(arr[0]);
minimumDifference(arr, N);
return 0;
}
// This code is contributed by divyesh072019.
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to minimize array
// elements by shift operations
static int minimize(int n)
{
int optEle = n;
String strEle = Integer.toString(n);
// For checking all the
// left shift operations
for (int idx = 0; idx < strEle.length(); idx++)
{
// Left shift
int temp
= Integer.parseInt(strEle.substring(idx)
+ strEle.substring(0, idx));
// Consider the minimum possible value
optEle = Math.min(optEle, temp);
}
return optEle;
}
// Function to maximize array
// elements by shift operations
static int maximize(int n)
{
int optEle = n;
String strEle = Integer.toString(n);
// For checking all the
// left shift operations
for (int idx = 0; idx < strEle.length(); idx++)
{
// Left shift
int temp
= Integer.parseInt(strEle.substring(idx)
+ strEle.substring(0, idx));
// Consider the maximum possible value
optEle = Math.max(optEle, temp);
}
return optEle;
}
// Function to maximize the absolute
// difference between even and odd
// indexed array elements
static void minimumDifference(int[] arr)
{
int caseOne = 0;
int minVal = 0;
int maxVal = 0;
// To calculate the difference of
// odd indexed elements
// and even indexed elements
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;
// To calculate the difference
// between odd and even indexed
// array elements
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);
}
// Print the maximum value
System.out.println(Math.max(caseOne, caseTwo));
}
// Driver Code
public static void main(String[] args)
{
// Given array
int[] arr = { 332, 421, 215, 584, 232 };
minimumDifference(arr);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Function to minimize array
# elements by shift operations
def minimize(n):
optEle = n
strEle = str(n)
# For checking all the
# left shift operations
for idx in range(len(strEle)):
# Left shift
temp = int(strEle[idx:] + strEle[:idx])
# Consider the minimum possible value
optEle = min(optEle, temp)
return optEle
# Function to maximize array
# elements by shift operations
def maximize(n):
optEle = n
strEle = str(n)
# For checking all the
# left shift operations
for idx in range(len(strEle)):
# Left shift
temp = int(strEle[idx:] + strEle[:idx])
# Consider the maximum possible value
optEle = max(optEle, temp)
return optEle
# Function to maximize the absolute
# difference between even and odd
# indexed array elements
def minimumDifference(arr):
caseOne = 0
minVal = 0
maxVal = 0
# To calculate the difference of
# odd indexed elements
# and even indexed elements
for i in range(len(arr)):
if i % 2:
minVal += minimize(arr[i])
else:
maxVal += maximize(arr[i])
caseOne = abs(maxVal - minVal)
caseTwo = 0
minVal = 0
maxVal = 0
# To calculate the difference
# between odd and even indexed
# array elements
for i in range(len(arr)):
if i % 2:
maxVal += maximize(arr[i])
else:
minVal += minimize(arr[i])
caseTwo = abs(maxVal - minVal)
# Print the maximum value
print (max(caseOne, caseTwo))
# Given array
arr = [ 332, 421, 215, 584, 232 ]
minimumDifference(arr)
C#
// C# program for the above approach
using System;
class GFG
{
// Function to minimize array
// elements by shift operations
static int minimize(int n)
{
int optEle = n;
string strEle = n.ToString();
// For checking all the
// left shift operations
for (int idx = 0; idx < strEle.Length;idx++)
{
// Left shift
int temp = Int32.Parse(strEle.Substring(idx) +
strEle.Substring(0, idx));
// Consider the minimum possible value
optEle = Math.Min(optEle, temp);
}
return optEle;
}
// Function to maximize array
// elements by shift operations
static int maximize(int n)
{
int optEle = n;
string strEle = n.ToString();
// For checking all the
// left shift operations
for (int idx = 0; idx < strEle.Length;idx++)
{
// Left shift
int temp = Int32.Parse(strEle.Substring(idx) +
strEle.Substring(0, idx));
// Consider the maximum possible value
optEle = Math.Max(optEle, temp);
}
return optEle;
}
// Function to maximize the absolute
// difference between even and odd
// indexed array elements
static void minimumDifference(int[] arr)
{
int caseOne = 0;
int minVal = 0;
int maxVal = 0;
// To calculate the difference of
// odd indexed elements
// and even indexed elements
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;
// To calculate the difference
// between odd and even indexed
// array elements
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);
}
// Print the maximum value
Console.WriteLine(Math.Max(caseOne, caseTwo));
}
// Given array
public static void Main()
{
int[] arr = { 332, 421, 215, 584, 232 };
minimumDifference(arr);
}
}
// This code is contributed by chitranayal.
JavaScript
<script>
// Javascript program for the above approach
// Function to minimize array
// elements by shift operations
function minimize(n)
{
let optEle = n;
let strEle = (n).toString();
// For checking all the
// left shift operations
for (let idx = 0; idx < strEle.length; idx++)
{
// Left shift
let temp
= parseInt(strEle.substring(idx)
+ strEle.substring(0, idx));
// Consider the minimum possible value
optEle = Math.min(optEle, temp);
}
return optEle;
}
// Function to maximize array
// elements by shift operations
function maximize(n)
{
let optEle = n;
let strEle = n.toString();
// For checking all the
// left shift operations
for (let idx = 0; idx < strEle.length; idx++)
{
// Left shift
let temp
= parseInt(strEle.substring(idx)
+ strEle.substring(0, idx));
// Consider the maximum possible value
optEle = Math.max(optEle, temp);
}
return optEle;
}
// Function to maximize the absolute
// difference between even and odd
// indexed array elements
function minimumDifference(arr)
{
let caseOne = 0;
let minVal = 0;
let maxVal = 0;
// To calculate the difference of
// odd indexed elements
// and even indexed elements
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;
// To calculate the difference
// between odd and even indexed
// array elements
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);
}
// Print the maximum value
document.write(Math.max(caseOne, caseTwo)+"<Br>");
}
// Given array
let arr=[332, 421, 215, 584, 232 ];
minimumDifference(arr);
// This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize difference between odd and even-indexed array elements by rotating their binary representations Given an array arr[] consisting of N positive integers, the task is to find the maximum absolute difference between the sum of the array elements placed at even indices and those at odd indices of the array by rotating their binary representations any number of times. Consider only 8-bit representat
12 min read
Difference between sum of K maximum even and odd array elements Given an array arr[] and a number K, the task is to find the absolute difference of the sum of K maximum even and odd array elements.Note: At least K even and odd elements are present in the array respectively. Examples: Input arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output: 2Explanation:The 2 maximum even
8 min read
Maximize difference between sum of even and odd-indexed elements of a subsequence Given an array arr[] consisting of N positive integers, the task is to find the maximum possible difference between the sum of even and odd-indexed elements of a subsequence from the given array. Examples: Input: arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 } Output: 15 Explanation: Considering the subseq
7 min read
Maximize difference between odd and even indexed array elements by swapping unequal adjacent bits in their binary representations Given an array arr[] consisting of N positive integers, the task is to find the maximum absolute difference between the sum of the array elements placed at even and odd indices of the array by swapping unequal adjacent bits of in the binary representation of any array element any number of times. Ex
12 min read
Maximum difference between sum of even and odd indexed elements of a Subarray Given an array nums[] of size N, the task is to find the maximum difference between the sum of even and odd indexed elements of a subarray. Examples: Input: nums[] = {1, 2, 3, 4, -5}Output: 9Explanation: If we select the subarray {4, -5} the sum of even indexed elements is 4 and odd indexed element
11 min read