Jobs
Interviews

Solve Longest Alternating Subsequence using Lua Language

Solve Longest Alternating Subsequence using Lua to enhance your skills with lua coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Longest Alternating Subsequence

Difficulty : Medium

Categories :

  • Arrays

Given an array of integers, find the longest alternating subsequence. An alternating subsequence is a sequence of numbers where numbers in adjacent positions have opposite signs (positive/negative). For better understanding: if a[i] is positive, then a[i+1] should be negative and vice versa. Return the length of the longest such subsequence.

Constraints:

  • 1 ≤ arr.length ≤ 10⁵
  • -10⁶ ≤ arr[i] ≤ 10⁶
  • Zero (0) can be considered either positive or negative
  • Subsequence elements need not be consecutive in the original array

Examples:

Input: arr = [1,-2,6,4,-3,2,-4,5]
Output: 6
Explanation: The longest alternating subsequence is [1,-2,6,-3,2,-4]
Input: arr = [1,2,3,4,5]
Output: 2
Explanation: Any two adjacent numbers form an alternating sequence of length 2

Problem Solving

Input

What You'll Find Here

Real-World Applications Solve problems inspired by Lua's common use cases, such as game development and embedded systems.

Step-by-Step Guidance Break down Lua's concepts into digestible lessons.

Practical Skills Build hands-on experience with Lua for real-world projects.

Choose from the following categories