Longest alternating subsequence which has maximum sum of elements
Last Updated :
19 Dec, 2022
Given a list of length N with positive and negative integers. The task is to choose the longest alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite of the sign of the current element). Among all such subsequences, we have to choose one which has the maximum sum of elements and display that sum.
Examples:
Input: list = [-2 10 3 -8 -4 -1 5 -2 -3 1]
Output: 11
Explanation:
The largest subsequence with the greatest sum is [-2 10 -1 5 -2 1] with length 6.
Input: list=[12 4 -5 7 -9]
Output: 5
Explanation:
The largest subsequence with greatest sum is [12 -5 7 -9] with length 4.
Approach: The solution can be reached by the following approach:-
- To get alternating subsequences with maximum length and the largest sum, we will be traversing the whole list (length of list)-1 times for comparing signs of consecutive elements.
- During traversal, if we are getting more than 1 consecutive element of the same sign(exp. 1 2 4), then we will append the maximum element out of them to another list named large. so from 1, 2 and 4 we will append 4 to another list.
- If we have consecutive elements of opposite sign, we will simply add those elements to that list named large.
- Finally, the list named large will have the longest alternating subsequence with the largest elements.
- Now, we will have to calculate the sum of all elements from that list named large.
Lets take an example, we have a list [1, 2, 3, -2, -5, 1, -7, -1].
- In traversing this list length-1 times, we are getting 1, 2, 3 with the same sign so we will append greatest of these (i.e 3) to another list named large here.
Hence large=[3] - Now -2 and -5 have the same sign so we will append -2 to another List.
large=[3, -2] - Now, the sign of 1 and -7 are opposite, so we will append 1 to large.
large=[3, -2, 1] - For -7, -1 signs are same, Hence append -1 to large.
large=[3, -2, 1, -1] - Calculate the sum = 3 - 2 + 1 - 1 = 1
Below is the implementation of the above approach:
C++
// C++ implementation to find the
// longest alternating subsequence
// which has the maximum sum
#include<bits/stdc++.h>
using namespace std;
int calculateMaxSum(int n, int li[])
{
// Creating a temporary list ar to
// every time store same sign element
// to calculate maximum element from
// that list ar
vector<int> ar;
// Appending 1st element of list li
// to the ar
ar.push_back(li[0]);
// Creating list to store maximum
// values
vector<int> large;
for(int j = 0; j < n - 1; j++)
{
// If both number are positive
// then append (j + 1)th element
// to temporary list ar
if(li[j] > 0 and li[j + 1] > 0)
{
ar.push_back(li[j + 1]);
}
else if(li[j] > 0 and li[j + 1] < 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.push_back(*max_element(ar.begin(),
ar.end()));
// Empty ar list to re-append
// next elements
ar.clear();
ar.push_back(li[j + 1]);
}
else if(li[j] < 0 and li[j + 1] > 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.push_back(*max_element(ar.begin(),
ar.end()));
// Empty ar list to re-append
// next elements
ar.clear();
ar.push_back(li[j + 1]);
}
else
{
// If both number are negative
// then append (j + 1)th element
// to temporary list ar
ar.push_back(li[j + 1]);
}
}
// The final Maximum element in ar list
// also needs to be appended to large list
large.push_back(*max_element(ar.begin(),
ar.end()));
// Returning the sum of all elements
// from largest elements list with
// largest alternating subsequence size
int sum = 0;
for(int i = 0; i < large.size(); i++)
sum += large[i];
return sum;
}
// Driver code
int main()
{
int list[] = { -2, 8, 3, 8, -4, -15,
5, -2, -3, 1 };
int N = sizeof(list) / sizeof(list[0]);
cout << (calculateMaxSum(N, list));
}
// This code is contributed by Bhupendra_Singh
Java
// Java implementation to find the
// longest alternating subsequence
// which has the maximum sum
import java.util.*;
class GFG{
static int calculateMaxSum(int n, int li[])
{
// Creating a temporary list ar to
// every time store same sign element
// to calculate maximum element from
// that list ar
Vector<Integer> ar = new Vector<>();
// Appending 1st element of list li
// to the ar
ar.add(li[0]);
// Creating list to store maximum
// values
Vector<Integer> large = new Vector<>();
for(int j = 0; j < n - 1; j++)
{
// If both number are positive
// then append (j + 1)th element
// to temporary list ar
if(li[j] > 0 && li[j + 1] > 0)
{
ar.add(li[j + 1]);
}
else if(li[j] > 0 && li[j + 1] < 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.add(Collections.max(ar));
// Empty ar list to re-append
// next elements
ar.clear();
ar.add(li[j + 1]);
}
else if(li[j] < 0 && li[j + 1] > 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.add(Collections.max(ar));
// Empty ar list to re-append
// next elements
ar.clear();
ar.add(li[j + 1]);
}
else
{
// If both number are negative
// then append (j + 1)th element
// to temporary list ar
ar.add(li[j + 1]);
}
}
// The final Maximum element in ar list
// also needs to be appended to large list
large.add(Collections.max(ar));
// Returning the sum of all elements
// from largest elements list with
// largest alternating subsequence size
int sum = 0;
for(int i = 0; i < large.size(); i++)
sum += (int)large.get(i);
return sum;
}
// Driver code
public static void main(String args[])
{
int list[] = { -2, 8, 3, 8, -4, -15,
5, -2, -3, 1 };
int N = (list.length);
System.out.print(calculateMaxSum(N, list));
}
}
// This code is contributed by Stream_Cipher
Python3
# Python3 implementation to find the
# longest alternating subsequence
# which has the maximum sum
def calculateMaxSum(n, li):
# Creating a temporary list ar to every
# time store same sign element to
# calculate maximum element from
# that list ar
ar =[]
# Appending 1st element of list li
# to the ar
ar.append(li[0])
# Creating list to store maximum
# values
large =[]
for j in range(0, n-1):
# If both number are positive
# then append (j + 1)th element
# to temporary list ar
if(li[j]>0 and li[j + 1]>0):
ar.append(li[j + 1])
elif(li[j]>0 and li[j + 1]<0):
# If opposite elements found
# then append maximum element
# to large list
large.append(max(ar))
# Empty ar list to re-append
# next elements
ar =[]
ar.append(li[j + 1])
elif(li[j]<0 and li[j + 1]>0):
# If opposite elements found
# then append maximum element
# to large list
large.append(max(ar))
# Empty ar list to re-append
# next elements
ar =[]
ar.append(li[j + 1])
else:
# If both number are negative
# then append (j + 1)th element
# to temporary list ar
ar.append(li[j + 1])
# The final Maximum element in ar list
# also needs to be appended to large list
large.append(max(ar))
# returning the sum of all elements
# from largest elements list with
# largest alternating subsequence size
return sum(large)
# Driver code
list =[-2, 8, 3, 8, -4, -15, 5, -2, -3, 1]
N = len(list)
print(calculateMaxSum(N, list))
C#
// C# implementation to find the
// longest alternating subsequence
// which has the maximum sum
using System;
using System.Collections.Generic;
class GFG{
static int find_max(List<int> ar)
{
int mx = -1000000;
foreach(var i in ar)
{
if(i > mx)
mx = i;
}
return mx;
}
static int calculateMaxSum(int n, int []li)
{
// Creating a temporary list ar to
// every time store same sign element
// to calculate maximum element from
// that list ar
List<int> ar = new List<int>();
// Appending 1st element of list li
// to the ar
ar.Add(li[0]);
// Creating list to store maximum
// values
List<int> large = new List<int>();
for(int j = 0; j < n - 1; j++)
{
// If both number are positive
// then append (j + 1)th element
// to temporary list ar
if(li[j] > 0 && li[j + 1] > 0)
{
ar.Add(li[j + 1]);
}
else if(li[j] > 0 && li[j + 1] < 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.Add(find_max(ar));
// Empty ar list to re-append
// next elements
ar.Clear();
ar.Add(li[j + 1]);
}
else if(li[j] < 0 && li[j + 1] > 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.Add(find_max(ar));
// Empty ar list to re-append
// next elements
ar.Clear();
ar.Add(li[j + 1]);
}
else
{
// If both number are negative
// then append (j + 1)th element
// to temporary list ar
ar.Add(li[j + 1]);
}
}
// The final Maximum element in ar list
// also needs to be appended to large list
large.Add(find_max(ar));
// Returning the sum of all elements
// from largest elements list with
// largest alternating subsequence size
int sum = 0;
foreach(var i in large)
{
sum += i;
}
return sum;
}
// Driver code
public static void Main()
{
int []list = { -2, 8, 3, 8, -4, -15,
5, -2, -3, 1 };
int N = (list.Length);
Console.WriteLine(calculateMaxSum(N, list));
}
}
// This code is contributed by Stream_Cipher
JavaScript
<script>
// Javascript implementation to find the
// longest alternating subsequence
// which has the maximum sum
function find_max(ar)
{
let mx = -1000000;
for(let i = 0; i < ar.length; i++)
{
if(ar[i] > mx)
mx = ar[i];
}
return mx;
}
function calculateMaxSum(n, li)
{
// Creating a temporary list ar to
// every time store same sign element
// to calculate maximum element from
// that list ar
let ar = [];
// Appending 1st element of list li
// to the ar
ar.push(li[0]);
// Creating list to store maximum
// values
let large = [];
for(let j = 0; j < n - 1; j++)
{
// If both number are positive
// then append (j + 1)th element
// to temporary list ar
if(li[j] > 0 && li[j + 1] > 0)
{
ar.push(li[j + 1]);
}
else if(li[j] > 0 && li[j + 1] < 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.push(find_max(ar));
// Empty ar list to re-append
// next elements
ar = [];
ar.push(li[j + 1]);
}
else if(li[j] < 0 && li[j + 1] > 0)
{
// If opposite elements found
// then append maximum element
// to large list
large.push(find_max(ar));
// Empty ar list to re-append
// next elements
ar = [];
ar.push(li[j + 1]);
}
else
{
// If both number are negative
// then append (j + 1)th element
// to temporary list ar
ar.push(li[j + 1]);
}
}
// The final Maximum element in ar list
// also needs to be appended to large list
large.push(find_max(ar));
// Returning the sum of all elements
// from largest elements list with
// largest alternating subsequence size
let sum = 0;
for(let i = 0; i < large.length; i++)
{
sum += large[i];
}
return sum;
}
let list = [ -2, 8, 3, 8, -4, -15, 5, -2, -3, 1 ];
let N = (list.length);
document.write(calculateMaxSum(N, list));
</script>
Time Complexity:O(N2)
Auxiliary Space: O(N)
Similar Reads
Longest alternating subsequence with maximum sum | Set 2 Given an array arr[] of size N, consisting of positive and negative integers, the task is to find the longest alternating subsequence(i.e. the sign of every element is opposite to that of its previous element) from the given array which has the maximum sum.Examples: Input: arr[] = {-2, 10, 3, -8, -4
7 min read
Maximum sum subsequence with at-least k distant elements Given an array and a number k, find a subsequence such that Sum of elements in subsequence is maximumIndices of elements of subsequence differ atleast by k Examples Input : arr[] = {4, 5, 8, 7, 5, 4, 3, 4, 6, 5} k = 2 Output: 19 Explanation: The highest value is obtained if you pick indices 1, 4, 7,
6 min read
Maximum sum subsequence of any size which is decreasing-increasing alternatively Given an array of integers arr[], find the subsequence with maximum sum whose elements are first decreasing, then increasing, or vice versa, The subsequence can start anywhere in the main sequence, not necessarily at the first element of the main sequence. A sequence {x1, x2, .. xn} is an alternatin
15+ min read
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence Given an array arr[], the task is to find the maximum sum of all the elements which are not a part of the longest increasing subsequence. Examples: Input: arr[] = {4, 6, 1, 2, 3, 8} Output: 10 Explanation: Elements are 4 and 6 Input: arr[] = {5, 4, 3, 2, 1} Output: 14 Explanation: Elements are 5, 4,
8 min read
Longest subsequence having maximum sum Given an array arr[] of size N, the task is to find the longest non-empty subsequence from the given array whose sum is maximum. Examples: Input: arr[] = { 1, 2, -4, -2, 3, 0 } Output: 1 2 3 0 Explanation: Sum of elements of the subsequence {1, 2, 3, 0} is 6 which is the maximum possible sum. Theref
8 min read