BackmediumArrays Swiggy

Galactic Trade Route Optimization 3 Solution

Problem Statement

In a distant galaxy, spaceships travel through various trade routes, each with a different commodity multiplier. Write a function to find the maximum product of a subroute within a given array of commodity multipliers, where the subroute must have at least one space station.

Example 1
Input
[5, 3, -4, 5]
Output
60

Explanation: Step-by-step: with input [5, 3, -4, 5], we first find all subroutes: [5], [5, 3], [5, 3, -4], [5, 3, -4, 5], [3], [3, -4], [3, -4, 5], [-4], [-4, 5], [5]. Then we calculate the product of each subroute: 5, 15, -60, -120, 3, 12, -20, -4, 5. Finally, we find the maximum product, which is 60.

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

Explanation: Step-by-step: with input [5, 2, -1, 3], we first find all subroutes: [5], [5, 2], [5, 2, -1], [5, 2, -1, 3], [2], [2, -1], [2, -1, 3], [-1], [-1, 3], [3]. Then we calculate the product of each subroute: 5, 10, -10, -30, 2, -2, -6, -1, 3. Finally, we find the maximum product, which is 15.

Constraints

  • The input array will have at least one element.
  • The input array will contain only integers between -100 and 100.
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