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 from 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.
Input: list = [-2 10 3 -8 -4 -1 5 -2 -3 1]
The largest subsequence with the greatest sum is [-2 10 -1 5 -2 1] with length 6.
Input: list=[12 4 -5 7 -9]
The largest subsequence with greatest sum is [12 -5 7 -9] with length 4.
Approach: The solution can be reached by following approach:-
- For getting alternating subsequence with maximum length and 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 elements of 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 element of opposite sign we will simply add those elements to that list named large.
- Finally the list named large will have longest alternating subsequence with 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.
- Now -2 and -5 have the same sign so we will append -2 to another List.
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest alternating subsequence with maximum sum | Set 2
- Maximum length Subsequence with alternating sign and maximum Sum
- Longest alternating subsequence in terms of positive and negative integers
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
- Length of largest subsequence consisting of a pair of alternating digits
- Longest alternating sub-array starting from every index in a Binary Array
- Longest alternating (positive and negative) subarray starting at every index
- Length of the longest alternating subarray
- Length of the longest alternating even odd subarray
- Rearrange a given list such that it consists of alternating minimum maximum elements
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Check if the array has an element which is equal to sum of all the remaining elements
- Longest increasing subsequence which forms a subarray in the sorted representation of the array
- Maximum subsequence sum from a given array which is a perfect square
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Find alphabet in a Matrix which has maximum number of stars around it
- Check if the array has an element which is equal to product of remaining elements
- Check if the array has an element which is equal to XOR of remaining elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.