BackmediumArrays Paytm

Optimize Galactic Trade Routes Solution

Problem Statement

Given a list of space stations with their respective trade values, determine the maximum total trade value that can be achieved by either keeping the trade value of a station as is or replacing it with the difference between the total trade values of all previous stations and all subsequent stations.

Example 1
Input
[25, 22, 7, 11, 2]
Output
25

Explanation: Step-by-step calculation: replace the first station's trade value with 25, and the last station's trade value with 25-22 = 3, and the second station's trade value with 25-3 = 22, and the third station's trade value with 22-7 = 15, and the fourth station's trade value with 22-11 = 11, and the fifth station's trade value with 15-2 = 13, giving output 25

Example 2
Input
[10, 5, 8, 3, 1]
Output
10

Explanation: Step-by-step calculation: replace the first station's trade value with 10, and the last station's trade value with 10-5 = 5, and the second station's trade value with 10-5 = 5, and the third station's trade value with 5-8 = -3, and the fourth station's trade value with 5-3 = 2, and the fifth station's trade value with 5-1 = 4, giving output 10

Constraints

  • 1 <= number of space stations <= 1000
  • -10000 <= trade value of each station <= 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