BackmediumArrays Uber

Financial Trends Solution

Problem Statement

Given an array of integers representing daily stock prices, determine the length of the longest bull-run and bear-run sequence, defined as an increasing sequence of prices followed by a decreasing sequence of prices.

Example 1
Input
[12, 15, 18, 20, 18, 15, 12]
Output
3

Explanation: Step-by-step: with input [12, 15, 18, 20, 18, 15, 12], we first find the longest increasing sequence [12, 15, 18] with length 3. Then, we find the longest decreasing sequence [18, 15, 12] with length 3. The longest bull-run and bear-run sequence is [12, 15, 18] and [18, 15, 12] respectively with length 3.

Example 2
Input
[5, 7, 9, 10, 12, 10, 8, 6]
Output
4

Explanation: Step-by-step: with input [5, 7, 9, 10, 12, 10, 8, 6], we first find the longest increasing sequence [5, 7, 9, 10] with length 4. Then, we find the longest decreasing sequence [12, 10, 8, 6] with length 4. The longest bull-run and bear-run sequence is [5, 7, 9, 10] and [12, 10, 8, 6] respectively with length 4 and 4 respectively.

Constraints

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