Given an **array arr[]** of size n having both positive and negative integer excluding zero. Find the maximum sum of maximum size alternate subsequence of a given sequence that is, in a subsequence sign of each adjacent element is opposite for example if the first one is positive then the second one has to be negative followed by another positive integer and so on.**Examples:**

Input :arr[] = {1, 2, 3, 4, -1, -2}Output :3Explanation:

Here, maximum size subsequences are [1, -1] [1, -2] [2, -1] [2, -2] [3, -1]

[3, -2] [4, -1] [4, -2] but the subsequence which have maximum sum is [4, -1]

and the sum of this subsequence is 3. Hence, the output is 3.

Input :arr[] = {-1, -6, 12, -4, -5}Output :7Explanation:

Here, maximum size subsequences are [-1, 12, -4] [-1, 12, -5] [-6, -12, -4]

[-6, 12, -5] but the subsequence which have maximum sum is [-1, 12, -4]

and the sum of this subsequence is 7. Hence, the output is 7.

**Naive approach:**

To solve the problem mentioned above the simple approach is to find all the alternating subsequences and their sum and return the maximum sum among all of them. **Efficient Approach:**

The efficient approach to solve the above problem is to pick the maximum element among continuous positive and continuous negative elements every time and add it to the maximum sum so far. The variable which stores the maximum sum will hold the final answer.

Below is the implementation of the above-mentioned approach:

## Java

`// Java program to find the maximum ` `// alternating subsequence sum for ` `// a given array ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find maximum ` `// alternating subequence sum ` `static` `int` `maxAlternatingSum(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` ` ` `// Initialize sum to 0 ` ` ` `int` `max_sum = ` `0` `; ` ` ` ` ` `int` `i = ` `0` `; ` ` ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `int` `current_max = arr[i]; ` ` ` `int` `k = i; ` ` ` ` ` `while` `(k < n && ` ` ` `((arr[i] > ` `0` `&& arr[k] > ` `0` `) || ` ` ` `(arr[i] < ` `0` `&& arr[k] < ` `0` `))) ` ` ` `{ ` ` ` `current_max = Math.max(current_max, arr[k]); ` ` ` `k += ` `1` `; ` ` ` `} ` ` ` ` ` `// Calculate the sum ` ` ` `max_sum += current_max; ` ` ` ` ` `i = k; ` ` ` `} ` ` ` ` ` `// Return the final result ` ` ` `return` `max_sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` ` ` `// Array initialization ` ` ` `int` `[] arr = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, -` `1` `, -` `2` `}; ` ` ` ` ` `// Length of array ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(maxAlternatingSum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the maximum alternating ` `# subsequence sum for a given array ` ` ` `# Function to find maximum ` `# alternating subequence sum ` `def` `maxAlternatingSum(arr, n): ` ` ` ` ` `# initialize sum to 0 ` ` ` `max_sum ` `=` `0` ` ` ` ` `i ` `=` `0` ` ` ` ` `while` `i<n: ` ` ` `current_max ` `=` `arr[i] ` ` ` `k ` `=` `i ` ` ` ` ` `while` `k<n ` `and` `((arr[i]>` `0` `and` `arr[k]>` `0` `) ` ` ` `or` `(arr[i]<` `0` `and` `arr[k]<` `0` `)): ` ` ` ` ` `current_max ` `=` `max` `(current_max, arr[k]) ` ` ` ` ` `k` `+` `=` `1` ` ` ` ` `# calculate the sum ` ` ` `max_sum` `+` `=` `current_max ` ` ` ` ` `i ` `=` `k ` ` ` ` ` `# return the final result ` ` ` `return` `max_sum ` ` ` ` ` `# Driver code ` ` ` `# array initiaisation ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `-` `1` `, ` `-` `2` `] ` ` ` `# length of array ` `n ` `=` `len` `(arr) ` ` ` `print` `(maxAlternatingSum(arr, n)) ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity :** O(n)

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.

## Recommended Posts:

- Longest subsequence consisting of alternate vowels and consonants
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Find maximum Subsequence Sum according to given conditions
- Maximum subsequence sum from a given array which is a perfect square
- Maximum length Subsequence with alternating sign and maximum Sum
- Find the equal pairs of subsequence of S and subsequence of T
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Find the subsequence with given sum in a superincreasing sequence
- Maximize the Sum of a Subsequence from an Array based on given conditions
- Find Sum of all unique sub-array sum for a given array.
- Find an element in array such that sum of left array is equal to sum of right array
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Sum of series with alternate signed squares of AP
- Sum of alternate elements of a N x N matrix
- Sum of Fibonacci Numbers with alternate negatives
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.