BackmediumArrays Microsoft

Galactic Signal Patterns Solution

Problem Statement

Given an array of signal strengths from a distant planet, determine the length of the longest subsequence where the signal strengths alternate between increasing and decreasing order, helping astronomers identify stable communication windows.

Example 1
Input
[5, 8, 6, 9, 5]
Output
2

Explanation: Step-by-step: Given the input [5, 8, 6, 9, 5], we can see that the longest subsequence where the signal strengths alternate between increasing and decreasing order is [5, 8, 6] or [6, 9, 5]. Both subsequences have a length of 3, but we need to find the longest valid subsequence. In this case, the longest valid subsequence is [5, 8, 6] or [6, 9, 5] with length 3.

Example 2
Input
[1, 3, 2, 4]
Output
3

Explanation: Step-by-step: Given the input [1, 3, 2, 4], we can see that the longest subsequence where the signal strengths alternate between increasing and decreasing order is [1, 3, 2, 4]. This subsequence has a length of 4, and it is valid. Therefore, the longest valid subsequence is [1, 3, 2, 4] with length 4.

Constraints

  • 1 <= array length <= 1000
  • -1000 <= each element in the array <= 1000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free