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.
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.
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.
Master coding challenges related to Arrays and solve the [Backup] Galactic Trade Route Optimization problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak