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 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++
#include <bits/stdc++.h>
using namespace std;
int maxPossibleDiff(vector< int >& arr, int N)
{
arr.push_back(-1);
reverse(arr.begin(), arr.end());
arr.push_back(-1);
reverse(arr.begin(), arr.end());
int maxDiff = 0;
for ( int i = 1; i <= N; i++) {
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1]) {
maxDiff += arr[i];
}
if (arr[i] < arr[i - 1]
&& arr[i] < arr[i + 1]) {
maxDiff -= arr[i];
}
}
cout << maxDiff;
}
int main()
{
vector< int > arr = { 3, 2, 1, 4, 5,
2, 1, 7, 8, 9 };
int N = arr.size();
maxPossibleDiff(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxPossibleDiff(Vector<Integer> arr, int N)
{
arr.add(- 1 );
Collections.reverse(arr);
arr.add(- 1 );
Collections.reverse(arr);
int maxDiff = 0 ;
for ( int i = 1 ; i <= N; i++)
{
if (arr.get(i) > arr.get(i - 1 ) &&
arr.get(i) > arr.get(i + 1 ))
{
maxDiff += arr.get(i);
}
if (arr.get(i) < arr.get(i - 1 ) &&
arr.get(i) < arr.get(i + 1 ))
{
maxDiff -= arr.get(i);
}
}
System.out.print(maxDiff);
}
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);
}
int N = v.size();
maxPossibleDiff(v, N);
}
}
|
Python3
def maxPossibleDiff(arr, N):
arr.append( - 1 )
arr = arr[:: - 1 ]
arr.append( - 1 )
arr = arr[:: - 1 ]
maxDiff = 0
for i in range ( 1 ,N + 1 ):
if (arr[i] > arr[i - 1 ] and arr[i] > arr[i + 1 ]):
maxDiff + = arr[i]
if (arr[i] < arr[i - 1 ] and arr[i] < arr[i + 1 ]):
maxDiff - = arr[i]
print (maxDiff)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 ]
N = len (arr)
maxPossibleDiff(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void maxPossibleDiff(List< int > arr, int N)
{
arr.Add(-1);
arr.Reverse();
arr.Add(-1);
arr.Reverse();
int maxDiff = 0;
for ( int i = 1; i <= N; i++)
{
if (arr[i] > arr[i - 1] &&
arr[i] > arr[i + 1])
{
maxDiff += arr[i];
}
if (arr[i] < arr[i - 1] &&
arr[i] < arr[i + 1])
{
maxDiff -= arr[i];
}
}
Console.Write(maxDiff);
}
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);
}
int N = v.Count;
maxPossibleDiff(v, N);
}
}
|
Javascript
<script>
function maxPossibleDiff(arr, N)
{
arr.push(-1);
arr.reverse();
arr.push(-1);
arr.reverse();
var maxDiff = 0;
for ( var i = 1; i <= N; i++) {
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1]) {
maxDiff += arr[i];
}
if (arr[i] < arr[i - 1]
&& arr[i] < arr[i + 1]) {
maxDiff -= arr[i];
}
}
document.write( maxDiff);
}
var arr = [3, 2, 1, 4, 5,
2, 1, 7, 8, 9];
var N = arr.length;
maxPossibleDiff(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...