BackmediumArraysUberRazorpay

Longest Alternating Sequences Solution

Problem Statement

Given an array of integers values, determine the length of the longest increasing-decreasing sequence and the longest decreasing-increasing sequence.

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

Explanation: Step-by-step: with input [1, 2, 3, 2, 1, 5, 5, 5], we first initialize two arrays, one for increasing-decreasing sequence and one for decreasing-increasing sequence. Then we iterate through the input array, and for each element, we check if it is greater than the last element in the increasing-decreasing sequence array, if so, we append it to the increasing-decreasing sequence array. We do the same for the decreasing-increasing sequence array. Finally, we return the maximum length of the two arrays.

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

Explanation: Step-by-step: with input [5, 5, 5], we first initialize two arrays, one for increasing-decreasing sequence and one for decreasing-increasing sequence. Then we iterate through the input array, and for each element, we check if it is greater than the last element in the increasing-decreasing sequence array, if so, we append it to the increasing-decreasing sequence array. We do the same for the decreasing-increasing sequence array. Finally, we return the maximum length of the two arrays.

Constraints

  • 2 <= length of input array <= 1000
  • -1000 <= each element in the input 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

Longest Alternating Sequences — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers values, determine the length of the longest increasing-decreasing sequence and the longest decreasing-increasing sequence.

Examples

Example 1

Input

[1, 2, 3, 2, 1, 5, 5, 5]

Output

[5, 5]

Explanation: Step-by-step: with input [1, 2, 3, 2, 1, 5, 5, 5], we first initialize two arrays, one for increasing-decreasing sequence and one for decreasing-increasing sequence. Then we iterate through the input array, and for each element, we check if it is greater than the last element in the increasing-decreasing sequence array, if so, we append it to the increasing-decreasing sequence array. We do the same for the decreasing-increasing sequence array. Finally, we return the maximum length of the two arrays.

Example 2

Input

[5, 5, 5]

Output

[3, 3]

Explanation: Step-by-step: with input [5, 5, 5], we first initialize two arrays, one for increasing-decreasing sequence and one for decreasing-increasing sequence. Then we iterate through the input array, and for each element, we check if it is greater than the last element in the increasing-decreasing sequence array, if so, we append it to the increasing-decreasing sequence array. We do the same for the decreasing-increasing sequence array. Finally, we return the maximum length of the two arrays.

Constraints

  • 2 <= length of input array <= 1000
  • -1000 <= each element in the input array <= 1000

Optimal Approach & Strategy

The optimal approach involves iterating over the array to identify increasing and decreasing sequences in a single pass, resulting in a time complexity of O(n). This can be achieved by maintaining two variables to track the length of the longest bull-run and bear-run sequences.

Brute Force Approach

One naive approach is to compare each element with every other element to identify increasing and decreasing sequences, resulting in a time complexity of O(n²). This approach is inefficient and should be avoided for large inputs. A better approach would involve iterating over the array only once.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestAlternatingSequences(values) {
  if (values.length === 0) return [0, 0];
  let bullRun = 1, bearRun = 1, maxBullRun = 1, maxBearRun = 1;
  for (let i = 1; i < values.length; i++) {
    if (values[i] > values[i - 1]) {
      bullRun += 1;
      maxBullRun = Math.max(maxBullRun, bullRun);
      bearRun = 1;
    } else if (values[i] < values[i - 1]) {
      bearRun += 1;
      maxBearRun = Math.max(maxBearRun, bearRun);
      bullRun = 1;
    } else {
      bullRun = 1;
      bearRun = 1;
    }
  }
  return [Math.max(maxBullRun, values.length), Math.max(maxBearRun, values.length)];
}

Asked in Top Tech Interviews

UberRazorpay

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.