Dsa
Dynamic Programming
Easy
Longest Alternating Subsequence
Given an array of integers, find the length of the longest alternating subsequence. An alternating subsequence is defined as a subsequence in which the differences between successive elements are strictly positive or strictly negative.
Input:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of elements in the array.
- The second line contains n space-separated integers, the elements of the array.
Output:
- A single integer, representing the length of the longest alternating subsequence.
Example:
Input:
6
1 5 3 4 2 6
Output:
5
Explanation:
The longest alternating subsequence is [1, 5, 3, 4, 2] or [1, 3, 4, 2, 6], both are of length 5.
Key concepts
dynamic_programmingsubsequencearray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now