The 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 the longest subsequence with alternate low and high elements.

For example, consider array A[] = [8, 9, 6, 4, 5, 7, 3, 2, 4]. The longest alternating subsequence length is 6, and the subsequence is [8, 9, 6, 7, 3, 4] as (8 < 9 > 6 < 7 > 3 < 4).

Note that the longest alternating subsequence is not unique. Following are a 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…

Practice this problem

The problem differs from the problem of finding the Longest Alternating Subarray. Unlike subarrays, subsequences are not required to occupy consecutive positions within the original array.

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

  1. Include the element in the subsequence.
    • If the flag is true and A[i-1] < A[i], include A[i] as next high in the subsequence.
    • If the flag is false and A[i-1] > A[i], include A[i] as next low in the subsequence.

    Then recur for the next element by flipping the flag. If we get the longest subsequence by including the element in the subsequence, update the result.

  2. Exclude the element from subsequence.
     
    Exclude the current element and recur for next element (flag remains same). If we get the longest subsequence by excluding the element from the subsequence, update the result.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

Java


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

Python


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

The time complexity of the above solution is exponential and occupies space in the call stack.

 
The LAS problem has an optimal substructure and also exhibits overlapping subproblems. We know that problems with optimal substructure and overlapping subproblems can be solved by dynamic programming, in which subproblem solutions are saved rather than computed repeatedly. This method is demonstrated below in C++, Java, and Python, which follows a top-down approach using Memoization.

C++


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

Java


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

Python


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

The time complexity of the above top-down solution is O(n2) and requires O(n) extra space, where n is the size of the given sequence. We can even write a bottom-up version of the above memoized solution. The following code shows how to implement it in C, Java, and Python:

C


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

Java


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

Python


Download  Run Code

Output:

The length of the longest alternating subsequence is 6

The time complexity of the above bottom-up solution is O(n2) and requires O(n) extra space, where n is the size of the given sequence.

 
Also See:

Longest Alternating Subarray Problem