BackmediumArrays Flipkart

Fluctuating Stock Prices Solution

Problem Statement

Given a list of daily stock prices, determine if it's possible to make the prices fluctuate by buying or selling stocks on adjacent days. The prices are said to fluctuate if every day's price is either higher or lower than the previous day's price, and the higher and lower prices alternate.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we check if the prices are already in ascending order. Since they are, we return true because we can alternate between higher and lower prices by buying or selling stocks on adjacent days.

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

Explanation: Step-by-step: with input [5, 4, 3, 2, 1], we check if the prices are already in descending order. Since they are, we return true because we can alternate between higher and lower prices by buying or selling stocks on adjacent days.

Constraints

  • The input array will have a length between 2 and 1000.
  • The input array will contain only integers between 1 and 10000.
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