# Longest Alternating Subsequence Problem

Longest Alternating Subsequence is a problem of finding a subsequence of a given sequence in which the elements are in alternating order, and in which the sequence is as long as possible.

In order words, we need to find the length of longest subsequence with alternate low and high elements.

The problem differs from problem of finding longest alternating subarray. Unlike subarrays, subsequences are not required to occupy consecutive positions within the original sequences.

For example, consider array A[] = [8, 9, 6, 4, 5, 7, 3, 2, 4]

The length of longest subsequence is 6 and the subsequence is [8, 9, 6, 7, 3, 4] as
(8 < 9 > 6 < 7 > 3 < 4)

Note that the Longest Subsequence is not unique. Below are few more subsequences of length 6 –

(8, 9, 6, 7, 2, 4)
(8, 9, 4, 7, 3, 4)
(8, 9, 4, 7, 2, 4)

and many more..

We can use Recursion to solve this problem. The idea is to maintain a flag to indicate if next element in the sequence should be smaller or greater than the previous element. Then for any element arr[i] at index i, we have two choices –

1. We include the element in the subsequence.

• if the flag is true and arr[i-1] < arr[i], we include arr[i] as next high in the subsequence
• if the flag is false and arr[i-1] > arr[i], we include arr[i] as next low in the subsequence
Then we recurse for next element by flipping the flag. If we get longest subsequence by including the element in the subsequence, we update the result.

2. We exclude the element in subsequence.

We exclude the current element and recurse for next element (flag remains same). If we get longest subsequence by excluding the element in subsequence, we update the result.

## C++

Output:

The length of Longest Subsequence is 6

## Java

Output:

The length of Longest Subsequence is 6

The time complexity of above solution is exponential and auxiliary space used by the program is O(1).

The LAS problem has an optimal substructure and also exhibits overlapping subproblems. We know that problems having optimal substructure and overlapping subproblems can be solved by dynamic programming, in which subproblem solutions are saved rather than computed over and over again. This method is illustrated below which follows top-down approach using Memoization.

## C++

Output:

The length of Longest Subsequence is 6

## Java

Output:

The length of Longest Subsequence is 6

The time complexity of above solution is O(n2) and auxiliary space used by the program is O(n).

(2 votes, average: 5.00 out of 5)

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Subscribe
Notify of
Guest

Is any video tutorials available to understand the solution better?

Guest

I don’t understand why the solution needs a for loop, it works fine without it.
Also this dp solution works, O(n) complexity. Maybe I don’t understand something, but no matter what sequence I try, I can’t find a sequence where this solution would return a different result comparing to the solution from the article:

```int altSequenceDp(int arr[], int n) { ..int lastInc = 1; ..int lastDec = 1; ..for (int i = 1; i < n; i++) { ....if (arr[i] > arr[i - 1]) { ......lastInc = lastDec + 1; ....} else if (arr[i] < arr[i - 1]) { ......lastDec = lastInc + 1; ....} ..}```

``` ```

```..return max(lastInc, lastDec); }```

Guest
jasdasdasd

Think about this. What if the final element don’t need to be next to each other in its original index. Could you find the longest alternating subsequence?