Maximize difference between sum of even and odd-indexed elements of a subsequence
Last Updated :
20 May, 2021
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 subsequences { 3, 1, 5, 1, 9 } from the array
Sum of even-indexed array elements = 3 + 5 + 9 = 17
Sum of odd-indexed array elements = is 1 + 1 = 2
Therefore, the difference between the sum of even and odd-indexed elements present in the subsequence = (17 - 2) = 15, which is the maximum possible.
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 6
Naive Approach: The simplest approach to solve this problem is to generate all possible subsequences of the given array and for each subsequence, calculate the difference between the sum of even and odd indexed elements of the subsequence. Finally, print the maximum difference obtained.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to store the local maxima at even indices of the subsequence and store the local minima at odd indices of the subsequence. Finally, print the difference between the sum of even and odd indices of the subsequence.
Follow the steps below to solve the problem:
- Initialize a variable, say maxDiff, to store the maximum difference between the sum of even and odd-indexed elements of a subsequence.
- Traverse the array arr[] and check if arr[i] > arr[i + 1] and arr[i] < arr[i - 1] or not. If found to be true, then update maxDiff += arr[i].
- Otherwise, check if arr[i] > arr[i + 1] and arr[i] < arr[i - 1] or not. If found to be true, then update maxDiff -= arr[i].
- Finally, print the value of maxDiff.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum possible difference
// between sum of even and odd indices
int maxPossibleDiff(vector<int>& arr, int N)
{
// Convert arr[] into 1-based indexing
arr.push_back(-1);
// Reverse the array
reverse(arr.begin(), arr.end());
// Convert arr[] into 1 based index
arr.push_back(-1);
// Reverse the array
reverse(arr.begin(), arr.end());
// Stores maximum difference between
// sum of even and odd indexed elements
int maxDiff = 0;
// Traverse the array
for (int i = 1; i <= N; i++) {
// If arr[i] is local maxima
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1]) {
// Update maxDiff
maxDiff += arr[i];
}
// If arr[i] is local minima
if (arr[i] < arr[i - 1]
&& arr[i] < arr[i + 1]) {
// Update maxDiff
maxDiff -= arr[i];
}
}
cout << maxDiff;
}
// Driver Code
int main()
{
vector<int> arr = { 3, 2, 1, 4, 5,
2, 1, 7, 8, 9 };
// Size of array
int N = arr.size();
// Function Call
maxPossibleDiff(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the maximum possible
// difference between sum of even and
// odd indices
static void maxPossibleDiff(Vector<Integer> arr, int N)
{
// Convert arr[] into 1-based indexing
arr.add(-1);
// Reverse the array
Collections.reverse(arr);
// Convert arr[] into 1 based index
arr.add(-1);
// Reverse the array
Collections.reverse(arr);
// Stores maximum difference between
// sum of even and odd indexed elements
int maxDiff = 0;
// Traverse the array
for(int i = 1; i <= N; i++)
{
// If arr.get(i) is local maxima
if (arr.get(i) > arr.get(i - 1) &&
arr.get(i) > arr.get(i + 1))
{
// Update maxDiff
maxDiff += arr.get(i);
}
// If arr.get(i) is local minima
if (arr.get(i) < arr.get(i - 1) &&
arr.get(i) < arr.get(i + 1))
{
// Update maxDiff
maxDiff -= arr.get(i);
}
}
System.out.print(maxDiff);
}
// Driver Code
public static void main(String[] args)
{
int[] array = { 3, 2, 1, 4, 5,
2, 1, 7, 8, 9 };
Vector<Integer> v = new Vector<>();
for(int i :array)
{
v.add(i);
}
// Size of array
int N = v.size();
// Function Call
maxPossibleDiff(v, N);
}
}
// This code is contributed by shikhasingrajput
Python3
#Python3 program to implement
#the above approach
#Function to find the maximum possible difference
#between sum of even and odd indices
def maxPossibleDiff(arr, N):
#Convert arr[] o 1-based indexing
arr.append(-1)
#Reverse the array
arr = arr[::-1]
#Convert arr[] o 1 based index
arr.append(-1)
#Reverse the array
arr = arr[::-1]
#Stores maximum difference between
#sum of even and odd indexed elements
maxDiff = 0
#Traverse the array
for i in range(1,N+1):
#If arr[i] is local maxima
if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):
#Update maxDiff
maxDiff += arr[i]
#If arr[i] is local minima
if (arr[i] < arr[i - 1] and arr[i] < arr[i + 1]):
#Update maxDiff
maxDiff -= arr[i]
print (maxDiff)
#Driver Code
if __name__ == '__main__':
arr = [3, 2, 1, 4, 5, 2, 1, 7, 8, 9]
#Size of array
N = len(arr)
#Function Call
maxPossibleDiff(arr, N)
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the maximum possible
// difference between sum of even and
// odd indices
static void maxPossibleDiff(List<int> arr, int N)
{
// Convert []arr into 1-based indexing
arr.Add(-1);
// Reverse the array
arr.Reverse();
// Convert []arr into 1 based index
arr.Add(-1);
// Reverse the array
arr.Reverse();
// Stores maximum difference between
// sum of even and odd indexed elements
int maxDiff = 0;
// Traverse the array
for(int i = 1; i <= N; i++)
{
// If arr[i] is local maxima
if (arr[i] > arr[i - 1] &&
arr[i] > arr[i + 1])
{
// Update maxDiff
maxDiff += arr[i];
}
// If arr[i] is local minima
if (arr[i] < arr[i - 1] &&
arr[i] < arr[i + 1])
{
// Update maxDiff
maxDiff -= arr[i];
}
}
Console.Write(maxDiff);
}
// Driver Code
public static void Main(String[] args)
{
int[] array = { 3, 2, 1, 4, 5,
2, 1, 7, 8, 9 };
List<int> v = new List<int>();
foreach(int i in array)
{
v.Add(i);
}
// Size of array
int N = v.Count;
// Function Call
maxPossibleDiff(v, N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to find the maximum possible difference
// between sum of even and odd indices
function maxPossibleDiff(arr, N)
{
// Convert arr[] into 1-based indexing
arr.push(-1);
// Reverse the array
arr.reverse();
// Convert arr[] into 1 based index
arr.push(-1);
// Reverse the array
arr.reverse();
// Stores maximum difference between
// sum of even and odd indexed elements
var maxDiff = 0;
// Traverse the array
for (var i = 1; i <= N; i++) {
// If arr[i] is local maxima
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1]) {
// Update maxDiff
maxDiff += arr[i];
}
// If arr[i] is local minima
if (arr[i] < arr[i - 1]
&& arr[i] < arr[i + 1]) {
// Update maxDiff
maxDiff -= arr[i];
}
}
document.write( maxDiff);
}
// Driver Code
var arr = [3, 2, 1, 4, 5,
2, 1, 7, 8, 9];
// Size of array
var N = arr.length;
// Function Call
maxPossibleDiff(arr, N);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize difference between sum of even and odd-indexed elements of a subsequence | Set 2 Given an array arr[] consisting of N positive integers, the task is to find the maximum value of the difference between the sum of elements at even and odd indices for any subsequence of the array. Note: The value of N is always greater than 1. Examples: Input: arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9
11 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
Maximize difference between odd and even indexed array elements by shift operations 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: 658Explanation: C
9 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
Count possible removals to make absolute difference between the sum of odd and even indexed elements equal to K Given an array arr[] consisting of N integers and an integer K, the task is to find the number of times the absolute difference between the sum of elements at odd and even indices is K after removing any one element at a time from the given array. Examples: Input: arr[] = {2, 4, 2}, K = 2Output: 2Ex
15+ min read