githubEdit

18(July) Longest alternating subsequence

18. Longest Alternating Subsequence

The problem can be found at the following link: Question Linkarrow-up-right

Problem Description

You are given an array arr. Your task is to find the longest length of a good sequence. A good sequence {x1, x2, .. xn} is an alternating sequence if its elements satisfy one of the following relations:

  1. (x1 < x2 > x3 < x4 > x5 ...)

  2. (x1 > x2 < x3 > x4 < x5 ...)

Examples:

Input:

arr = [1, 5, 4]

Output:

3

Explanation: The entire sequence is a good sequence.

My Approach

  1. Initialization:

    • Check if the size of the array arr is less than 2. If true, return the size of the array as the longest length.

    • Initialize two variables up and down to 1, representing the length of the longest alternating subsequence ending with an increasing or decreasing element respectively.

  2. Alternating Subsequence Calculation:

    • Iterate through the array starting from the second element.

    • For each element, check if it is greater than the previous element. If true, update up as down + 1.

    • Otherwise, if it is smaller than the previous element, update down as up + 1.

  3. Return:

    • Return the maximum value between up and down, which gives the length of the longest alternating subsequence.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we iterate through the array once.

  • Expected Auxiliary Space Complexity: O(1), as we use a constant amount of additional space.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questionsarrow-up-right. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated