BackmediumArrays Swiggy

Galactic Trade Route Optimization 2 Solution

Problem Statement

In a galaxy with fluctuating resource prices, find the maximum profit that can be achieved by trading resources along a route where the price difference between each pair of consecutive resources alternates between positive and negative. If no such route exists, return 0.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we calculate the differences between consecutive elements: [1, 1, 1, 1]. Since the signs do not alternate, the maximum profit is 0.

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

Explanation: Step-by-step: with input [5, 4, 3, 2, 1], we calculate the differences between consecutive elements: [-1, -1, -1, -1]. Since the signs do not alternate, the maximum profit is 0.

Constraints

  • The input array contains at least 2 and no more than 1000 elements.
  • Each element in the array is between -1000 and 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