Longest Alternating Subsequence problem 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, find the length of longest subsequence with alternate low and high elements.

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

2. We exclude the element in subsequence.

**C++ implementation –**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <bits/stdc++.h> using namespace std; // Util function to find length of longest subsequence // if flag is true, next element should be greater // if flag is false, next element should be smaller int Util(int arr[], int start, int end, bool flag) { int res = 0; for (int i = start; i <= end; i++) { // include arr[i] as next high in subsequence and flip flag // for next subsequence if (flag && arr[i - 1] < arr[i]) res = max(res, 1 + Util(arr, i + 1, end, !flag)); // include arr[i] as next low in subsequence and flip flag // for next subsequence else if (!flag && arr[i - 1] > arr[i]) res = max(res, 1 + Util(arr, i + 1, end, !flag)); // don't include arr[i] in subsequence else res = max(res, Util(arr, i + 1, end, flag)); } return res; } // Function to find length of longest subsequence with alternate // low and high elements. It uses Util() method. int findLongestSequence(int arr[], int n) { // Fix first element and recurse for remaining elements as first // element will always be part of longest subsequence (why?) // There are two possibilities - // 1. Next element is greater (pass true) // 2. Next element is smaller (pass false) return 1 + max(Util(arr, 1, n - 1, true), Util(arr, 1, n - 1, false)); // instead of fixing first element, we can also directly call // return max(Util(arr, 0, n, true), Util(arr, 0, n, false)); } // main function int main() { int arr[] = { 8, 9, 6, 4, 5, 7, 3, 2, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "The length of Longest Subsequence is " << findLongestSequence(arr, n); return 0; } |

**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 problem has an

**optimal substructure**as the problem can be broken down into smaller subproblems which can further be broken down into yet smaller subproblems, and so on. The problem also clearly exhibits

**overlapping subproblems**so we will end up solving the same subproblem over and over again. The repeated sub-problems can be seen by drawing recursion tree for values of n. 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 again and again. This method is illustrated below which follows top-down approach using

*Memo*ization.

**C++ implementation –**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#include <bits/stdc++.h> using namespace std; // define max number of elements in array #define N 10 // lookup table to store solutions of subproblem // max(lookup[i][0], lookup[i][1]) stores longest sequence // till arr[0..i] int lookup[N][2]; // Util function to find length of longest subsequence // if flag is true, next element should be greater // if flag is false, next element should be smaller int Util(int arr[], int start, int end, bool flag) { // if sub-problem is seen for the first time, solve it and // store its result in lookup table if (lookup[start][flag] == 0) { int res = 0; for (int i = start; i <= end; i++) { // include arr[i] as next high in subsequence and flip flag // for next subsequence if (flag && arr[i - 1] < arr[i]) res = max(res, 1 + Util(arr, i + 1, end, !flag)); // include arr[i] as next low in subsequence and flip flag // for next subsequence else if (!flag && arr[i - 1] > arr[i]) res = max(res, 1 + Util(arr, i + 1, end, !flag)); // don't include arr[i] in subsequence else res = max(res, Util(arr, i + 1, end, flag)); } lookup[start][flag] = res; } // return solution to current sub-problem return lookup[start][flag]; } // Function to find length of longest subsequence with alternate // low and high elements. It uses Util() method. int findLongestSequence(int arr[], int n) { // Fix first element and recurse for remaining elements. // There are two possibilities - // 1. Next element is greater (pass true) // 2. Next element is smaller (pass false) return 1 + max(Util(arr, 1, n - 1, true), Util(arr, 1, n - 1, false)); } // main function int main() { int arr[] = { 8, 9, 6, 4, 5, 7, 3, 2, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "The length of Longest Subsequence is " << findLongestSequence(arr, n); return 0; } |

**Output: **

The length of Longest Subsequence is 6

The time complexity of above solution is O(n^{2}) and auxiliary space used by the program is O(n).

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂