BackmediumArraysSalesforce

Alternating Sequence Length Solution

Problem Statement

Given an array of integers signal, determine the length of the longest subsequence where the elements alternate between increasing and decreasing order.

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

Explanation: Step-by-step: with input [1, 2, 3, 2, 1, 2, 3, 4, 3], we first initialize variables i and j to 0. We then iterate through the array, incrementing i when the current element is greater than the previous one and incrementing j when the current element is less than the previous one. The maximum of i and j is the length of the longest alternating subsequence.

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

Explanation: Step-by-step: with input [1, 3, 2, 4, 3], we first initialize variables i and j to 0. We then iterate through the array, incrementing i when the current element is greater than the previous one and incrementing j when the current element is less than the previous one. The maximum of i and j is the length of the longest alternating subsequence.

Constraints

  • 1 <= array length <= 1000
  • -1000 <= each element in the array <= 1000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Alternating Sequence Length — Problem Statement & Solution Guide

ArraysMediumIdentifying patterns in arrays
TimeO(n^2)
|
SpaceO(n)

Problem Description

Given an array of integers signal, determine the length of the longest subsequence where the elements alternate between increasing and decreasing order.

Examples

Example 1

Input

[1, 2, 3, 2, 1, 2, 3, 4, 3]

Output

5

Explanation: Step-by-step: with input [1, 2, 3, 2, 1, 2, 3, 4, 3], we first initialize variables i and j to 0. We then iterate through the array, incrementing i when the current element is greater than the previous one and incrementing j when the current element is less than the previous one. The maximum of i and j is the length of the longest alternating subsequence.

Example 2

Input

[1, 3, 2, 4, 3]

Output

5

Explanation: Step-by-step: with input [1, 3, 2, 4, 3], we first initialize variables i and j to 0. We then iterate through the array, incrementing i when the current element is greater than the previous one and incrementing j when the current element is less than the previous one. The maximum of i and j is the length of the longest alternating subsequence.

Constraints

  • 1 <= array length <= 1000
  • -1000 <= each element in the array <= 1000

Optimal Approach & Strategy

The optimal approach involves using dynamic programming to store the lengths of the longest alternating subsequences ending at each position. This approach has a time complexity of O(n) and a space complexity of O(n), making it more efficient for large arrays.

Brute Force Approach

The brute-force approach involves checking all possible subsequences of the given array and determining if they alternate between increasing and decreasing order. This approach has a time complexity of O(n²) due to the nested loops. However, it is not efficient for large arrays.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function longestAlternatingSubsequence(signal) { let n = signal.length; if (n === 0) return 0; let increasing = new Array(n).fill(1); let decreasing = new Array(n).fill(1); for (let i = 1; i < n; i++) { for (let j = 0; j < i; j++) { if (signal[i] > signal[j]) increasing[i] = Math.max(increasing[i], decreasing[j] + 1); if (signal[i] < signal[j]) decreasing[i] = Math.max(decreasing[i], increasing[j] + 1); if (signal[i] === signal[j]) { increasing[i] = Math.max(increasing[i], increasing[j]); decreasing[i] = Math.max(decreasing[i], decreasing[j]); } } } return Math.max(Math.max(...increasing), Math.max(...decreasing)); }

Asked in Top Tech Interviews

Salesforce

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.