# Maximum length Subsequence with alternating sign and maximum Sum

Given an array arr[] of size n having both positive and negative integer excluding zero. The task is to find the subsequence with an alternating sign having maximum size and maximum sum 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[] = {2, 3, 7, -6, -4}
Output: 7 -4
Explanation:
Possible subsequences are [2, -6] [2, -4] [3, -6] [3, -4] [7, -6] [7, -4].
Out of these [7, -4] has the maximum sum.

Input: arr[] = {-4, 9, 4, 11, -5, -17, 9, -3, -5, 2}
Output: -4 11 -5 9 -3 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

The main idea to solve the above problem is to find the maximum element from the segments of the array which consists of the same sign which means we have to pick the maximum element among continuous positive and continuous negative elements. As we want maximum size we will only take one element from each segment and also to maximize the sum, we need to take the maximum element of each segment.

Below is the implementation of the above approach:

## CPP

 `// C++ implementation to find the ` `// subsequence with alternating sign ` `// having maximum size and maximum sum. ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the subsequence ` `// with alternating sign having ` `// maximum size and maximum sum. ` `void` `findSubsequence(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `sign[n] = { 0 }; ` ` `  `    ``// Find whether each element ` `    ``// is positive or negative ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(arr[i] > 0) ` `            ``sign[i] = 1; ` `        ``else` `            ``sign[i] = -1; ` `    ``} ` ` `  `    ``int` `k = 0; ` `    ``int` `result[n] = { 0 }; ` ` `  `    ``// Find the required subsequence ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``int` `cur = arr[i]; ` `        ``int` `j = i; ` ` `  `        ``while` `(j < n && sign[i] == sign[j]) { ` ` `  `            ``// Find the maximum element ` `            ``// in the specified range ` `            ``cur = max(cur, arr[j]); ` `            ``++j; ` `        ``} ` ` `  `        ``result[k++] = cur; ` ` `  `        ``i = j - 1; ` `    ``} ` ` `  `    ``// print the result ` `    ``for` `(``int` `i = 0; i < k; i++) ` `        ``cout << result[i] << ``" "``; ` `    ``cout << ``"\n"``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// array declaration ` `    ``int` `arr[] = { -4, 9, 4, 11, -5, -17, 9, -3, -5, 2 }; ` ` `  `    ``// size of array ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``findSubsequence(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to find the ` `// subsequence with alternating sign ` `// having maximum size and maximum sum. ` `class` `GFG{ ` `  `  `// Function to find the subsequence ` `// with alternating sign having ` `// maximum size and maximum sum. ` `static` `void` `findSubsequence(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `sign[] = ``new` `int``[n]; ` `  `  `    ``// Find whether each element ` `    ``// is positive or negative ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `        ``if` `(arr[i] > ``0``) ` `            ``sign[i] = ``1``; ` `        ``else` `            ``sign[i] = -``1``; ` `    ``} ` `  `  `    ``int` `k = ``0``; ` `    ``int` `result[] = ``new` `int``[n]; ` `  `  `    ``// Find the required subsequence ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `  `  `        ``int` `cur = arr[i]; ` `        ``int` `j = i; ` `  `  `        ``while` `(j < n && sign[i] == sign[j]) { ` `  `  `            ``// Find the maximum element ` `            ``// in the specified range ` `            ``cur = Math.max(cur, arr[j]); ` `            ``++j; ` `        ``} ` `  `  `        ``result[k++] = cur; ` `  `  `        ``i = j - ``1``; ` `    ``} ` `  `  `    ``// print the result ` `    ``for` `(``int` `i = ``0``; i < k; i++) ` `        ``System.out.print(result[i]+ ``" "``); ` `    ``System.out.print(``"\n"``); ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``// array declaration ` `    ``int` `arr[] = { -``4``, ``9``, ``4``, ``11``, -``5``, -``17``, ``9``, -``3``, -``5``, ``2` `}; ` `  `  `    ``// size of array ` `    ``int` `n = arr.length; ` `  `  `    ``findSubsequence(arr, n); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## Python3

 `# Python3 implementation to find the ` `# subsequence with alternating sign ` `# having maximum size and maximum sum. ` ` `  `# Function to find the subsequence ` `# with alternating sign having ` `# maximum size and maximum sum. ` `def` `findSubsequence(arr, n): ` `    ``sign ``=` `[``0``]``*``n ` ` `  `    ``# Find whether each element ` `    ``# is positive or negative ` `    ``for` `i ``in` `range``(n): ` `        ``if` `(arr[i] > ``0``): ` `            ``sign[i] ``=` `1` `        ``else``: ` `            ``sign[i] ``=` `-``1` ` `  `    ``k ``=` `0` `    ``result ``=` `[``0``]``*``n ` ` `  `    ``# Find the required subsequence ` `    ``i ``=` `0` `    ``while` `i < n: ` ` `  `        ``cur ``=` `arr[i] ` `        ``j ``=` `i ` ` `  `        ``while` `(j < n ``and` `sign[i] ``=``=` `sign[j]): ` ` `  `            ``# Find the maximum element ` `            ``# in the specified range ` `            ``cur ``=` `max``(cur, arr[j]) ` `            ``j ``+``=` `1` ` `  `        ``result[k] ``=` `cur ` `        ``k ``+``=` `1` ` `  `        ``i ``=` `j ``-` `1` `        ``i ``+``=` `1` ` `  `    ``# print the result ` `    ``for` `i ``in` `range``(k): ` `        ``print``(result[i],end``=``" "``) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``# array declaration ` `    ``arr``=``[``-``4``, ``9``, ``4``, ``11``, ``-``5``, ``-``17``, ``9``, ``-``3``, ``-``5``, ``2``] ` ` `  `    ``# size of array ` `    ``n ``=` `len``(arr) ` ` `  `    ``findSubsequence(arr, n) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# implementation to find the ` `// subsequence with alternating sign ` `// having maximum size and maximum sum. ` `using` `System; ` ` `  `public` `class` `GFG{ ` `   `  `// Function to find the subsequence ` `// with alternating sign having ` `// maximum size and maximum sum. ` `static` `void` `findSubsequence(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `[]sign = ``new` `int``[n]; ` `   `  `    ``// Find whether each element ` `    ``// is positive or negative ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(arr[i] > 0) ` `            ``sign[i] = 1; ` `        ``else` `            ``sign[i] = -1; ` `    ``} ` `   `  `    ``int` `k = 0; ` `    ``int` `[]result = ``new` `int``[n]; ` `   `  `    ``// Find the required subsequence ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `   `  `        ``int` `cur = arr[i]; ` `        ``int` `j = i; ` `   `  `        ``while` `(j < n && sign[i] == sign[j]) { ` `   `  `            ``// Find the maximum element ` `            ``// in the specified range ` `            ``cur = Math.Max(cur, arr[j]); ` `            ``++j; ` `        ``} ` `   `  `        ``result[k++] = cur; ` `   `  `        ``i = j - 1; ` `    ``} ` `   `  `    ``// print the result ` `    ``for` `(``int` `i = 0; i < k; i++) ` `        ``Console.Write(result[i]+ ``" "``); ` `    ``Console.Write(``"\n"``); ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``// array declaration ` `    ``int` `[]arr = { -4, 9, 4, 11, -5, -17, 9, -3, -5, 2 }; ` `   `  `    ``// size of array ` `    ``int` `n = arr.Length; ` `   `  `    ``findSubsequence(arr, n); ` `} ` `} ` `// This code contributed by Rajput-Ji `

Output:

```-4 11 -5 9 -3 2
```

Time Complexity :O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.