# Longest alternating subsequence in terms of positive and negative integers

• Difficulty Level : Easy
• Last Updated : 16 Dec, 2022

Given an array arr[] of positive and negative numbers only. The task is to find the length of the longest alternating (means negative-positive-negative or positive-negative-positive) subsequence present in the array

Examples:

Input: arr[] = {-4, 3, -5, 9, 10, 12, 2, -1}
Output:
Explanation:
The longest sequence is {-4, 3, -5, 9, -1}, which is of length 5. There can be many more subsequences of this length.

Input: arr[] = {10, 12, 2, -1}
Output:
Explanation:
The longest sequence is {10, -1}, which is 2. There can be many more subsequences of this length.

Approach:
This problem can be solved using Dynamic Programming. It is a variation Longest Increasing Subsequence(LIS). The following are the steps:

1. For including and excluding an element in the given array arr[] for LAS(Longest Alternative Subsequence), a variable pos is used, when pos = true means the current element needs to be positive and if pos = false then current element needs to be negative.
2. If we include the current element, change the value of pos and recur for the next element because we need the next element of the opposite to the previously included element.
3. Now LAS[i][pos] can be recursively written as:
• Base case: If the index called recursively is greater than the last element, then return 0, as there is no such element left to form LAS and if LAS[i][pos] is calculated, then return the value.

if(i == N) {
return 0;

if(LAS[i][pos]) {
return LAS[i][pos];

• Recursive call: If the base case is not met, then recursively call when current element is included and excluded, then find the maximum of those to find LAS at that index.

LAS[i][pos] = Longest Alternating Subsequence at index i by including or excluding that element for the value of pos
LAS[i][pos] = max(1 + recursive_function(i+1, pos), recursive_function(i+1, pos));

• Return statement: At each recursive call(except the base case), return the value of LAS[i][pos]

return LAS[i][pos];

1. The LAS for the given array arr[] is the maximum of LAS[0][0] and LAS[0][1].

Below is the implementation of the above approach:

## C++

 `// C++ program to find the``// length of longest alternate``// subsequence``#include ``using` `namespace` `std;` `// LAS[i][pos] array to find``// the length of LAS till``// index i by including or``// excluding element arr[i]``// on the basis of value of pos``int` `LAS[1000][2] = { ``false` `};` `int` `solve(``int` `arr[], ``int` `n, ``int` `i, ``bool` `pos)``{``    ``// Base Case``    ``if` `(i == n)``        ``return` `0;` `    ``if` `(LAS[i][pos])``        ``return` `LAS[i][pos];` `    ``int` `inc = 0, exc = 0;` `    ``// If current element is``    ``// positive and pos is true``    ``// Include the current element``    ``// and change pos to false``    ``if` `(arr[i] > 0 && pos == ``true``) {``        ``pos = ``false``;` `        ``// Recur for the next``        ``// iteration``        ``inc = 1 + solve(arr, n, i + 1, pos);``    ``}` `    ``// If current element is``    ``// negative and pos is false``    ``// Include the current element``    ``// and change pos to true``    ``else` `if` `(arr[i] < 0 && pos == ``false``) {``        ``pos = ``true``;` `        ``// Recur for the next``        ``// iteration``        ``inc = 1 + solve(arr, n, i + 1, pos);``    ``}` `    ``// If current element is``    ``// excluded, recur for``    ``// next iteration``    ``exc = solve(arr, n, i + 1, pos);` `    ``LAS[i][pos] = max(inc, exc);` `    ``return` `LAS[i][pos];``}` `// Driver's Code``int` `main()``{``    ``int` `arr[] = { -1, 2, 3, 4, 5,``                  ``-6, 8, -99 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Print LAS``    ``cout << max(solve(arr, n, 0, 0),``                ``solve(arr, n, 0, 1));``}`

## Java

 `// Java program to find the``// length of longest alternate``// subsequence``class` `GFG {` `// LAS[i][pos] array to find``// the length of LAS till``// index i by including or``// excluding element arr[i]``// on the basis of value of pos``static` `int` `LAS[][] = ``new` `int``[``1000``][``2``];` `static` `int` `solve(``int` `arr[], ``int` `n, ``int` `i,``int` `pos)``{``    ` `    ``// Base Case``    ``if` `(i == n)``        ``return` `0``;` `    ``if` `(LAS[i][pos]== ``1``)``        ``return` `LAS[i][pos];` `    ``int` `inc = ``0``, exc = ``0``;` `    ``// If current element is``    ``// positive and pos is 1``    ``// Include the current element``    ``// and change pos to 0``    ``if` `(arr[i] > ``0` `&& pos == ``1``) {``        ``pos = ``0``;` `        ``// Recur for the next``        ``// iteration``        ``inc = ``1` `+ solve(arr, n, i + ``1``, pos);``    ``}` `    ``// If current element is``    ``// negative and pos is o``    ``// Include the current element``    ``// and change pos to 1``    ``else` `if` `(arr[i] < ``0` `&& pos == ``0``) {``        ``pos = ``1``;` `        ``// Recur for the next``        ``// iteration``        ``inc = ``1` `+ solve(arr, n, i + ``1``, pos);``    ``}` `    ``// If current element is``    ``// excluded, recur for``    ``// next iteration``    ``exc = solve(arr, n, i + ``1``, pos);` `    ``LAS[i][pos] = Math.max(inc, exc);` `    ``return` `LAS[i][pos];``}` `// Driver's Code``public` `static` `void` `main (String[] args)``{``    ``int` `arr[] = { -``1``, ``2``, ``3``, ``4``, ``5``, -``6``, ``8``, -``99` `};``    ``int` `n = arr.length;` `    ``// Print LAS``    ``System.out.println(Math.max(solve(arr, n, ``0``, ``0``),solve(arr, n, ``0``, ``1``)));``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 program to find the``# length of longest alternate``# subsequence``import` `numpy as np` `# LAS[i][pos] array to find``# the length of LAS till``# index i by including or``# excluding element arr[i]``# on the basis of value of pos``LAS ``=` `np.zeros((``1000``, ``2``))` `for` `i ``in` `range``(``1000``) :``    ``for` `j ``in` `range``(``2``) :``        ``LAS[i][j] ``=` `False` `def` `solve(arr, n, i, pos) :` `    ``# Base Case``    ``if` `(i ``=``=` `n) :``        ``return` `0``;` `    ``if` `(LAS[i][pos]) :``        ``return` `LAS[i][pos];` `    ``inc ``=` `0``; exc ``=` `0``;` `    ``# If current element is``    ``# positive and pos is true``    ``# Include the current element``    ``# and change pos to false``    ``if` `(arr[i] > ``0` `and` `pos ``=``=` `True``) :``        ``pos ``=` `False``;` `        ``# Recur for the next``        ``# iteration``        ``inc ``=` `1` `+` `solve(arr, n, i ``+` `1``, pos);` `    ``# If current element is``    ``# negative and pos is false``    ``# Include the current element``    ``# and change pos to true``    ``elif` `(arr[i] < ``0` `and` `pos ``=``=` `False``) :``        ``pos ``=` `True``;` `        ``# Recur for the next``        ``# iteration``        ``inc ``=` `1` `+` `solve(arr, n, i ``+` `1``, pos);``    ` `    ``# If current element is``    ``# excluded, recur for``    ``# next iteration``    ``exc ``=` `solve(arr, n, i ``+` `1``, pos);` `    ``LAS[i][pos] ``=` `max``(inc, exc);` `    ``return` `LAS[i][pos];` `# Driver's Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``-``1``, ``2``, ``3``, ``4``, ``5``, ``-``6``, ``8``, ``-``99` `];``    ``n ``=` `len``(arr);` `    ``# Print LAS``    ``print``(``max``(solve(arr, n, ``0``, ``0``), solve(arr, n, ``0``, ``1``)));``    ` `# This code is contributed by AnkitRai01`

## C#

 `// C# program to find the``// length of longest alternate``// subsequence` `using` `System;` `public` `class` `GFG {` `// LAS[i][pos] array to find``// the length of LAS till``// index i by including or``// excluding element arr[i]``// on the basis of value of pos``static` `int` `[,]LAS = ``new` `int``[1000,2];` `static` `int` `solve(``int` `[]arr, ``int` `n, ``int` `i,``int` `pos)``{``    ` `    ``// Base Case``    ``if` `(i == n)``        ``return` `0;` `    ``if` `(LAS[i,pos]== 1)``        ``return` `LAS[i,pos];` `    ``int` `inc = 0, exc = 0;` `    ``// If current element is``    ``// positive and pos is 1``    ``// Include the current element``    ``// and change pos to 0``    ``if` `(arr[i] > 0 && pos == 1) {``        ``pos = 0;` `        ``// Recur for the next``        ``// iteration``        ``inc = 1 + solve(arr, n, i + 1, pos);``    ``}` `    ``// If current element is``    ``// negative and pos is o``    ``// Include the current element``    ``// and change pos to 1``    ``else` `if` `(arr[i] < 0 && pos == 0) {``        ``pos = 1;` `        ``// Recur for the next``        ``// iteration``        ``inc = 1 + solve(arr, n, i + 1, pos);``    ``}` `    ``// If current element is``    ``// excluded, recur for``    ``// next iteration``    ``exc = solve(arr, n, i + 1, pos);` `    ``LAS[i,pos] = Math.Max(inc, exc);` `    ``return` `LAS[i,pos];``}` `// Driver's Code``public` `static` `void` `Main()``{``    ``int` `[]arr = { -1, 2, 3, 4, 5, -6, 8, -99 };``    ``int` `n = arr.Length;` `    ``// Print LAS``    ``Console.WriteLine(Math.Max(solve(arr, n, 0, 0),solve(arr, n, 0, 1)));``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

`5`

Time Complexity: O(N) where N is the length of the array.

Auxiliary Space: O(n) for call stack

My Personal Notes arrow_drop_up