A caravan of n vehicles crosses a desert in a single-file line, numbered 0 to n-1 from the front of the line to the back. Vehicle i carries fuel[i] litres of fuel. For every vehicle, define its ahead-total as the combined fuel carried by all vehicles strictly in front of it, and its behind-total as the combined fuel carried by all vehicles strictly behind it (a vehicle with nothing ahead of it, or nothing behind it, treats that missing total as 0). For each vehicle, report the absolute difference between its ahead-total and its behind-total.
Print n space-separated integers on one line: the absolute ahead/behind fuel difference for each vehicle, in order from vehicle 0 to vehicle n-1.
Example 1
Input
5 10 4 8 3 9
Expected
24 10 2 13 25
Explanation
Fuel is [10,4,8,3,9]. Vehicle 0: ahead=0, behind=4+8+3+9=24, diff=24. Vehicle 1: ahead=10, behind=8+3+9=20, diff=10. Vehicle 2: ahead=10+4=14, behind=3+9=12, diff=2. Vehicle 3: ahead=10+4+8=22, behind=9, diff=13. Vehicle 4: ahead=10+4+8+3=25, behind=0, diff=25. Output: 24 10 2 13 25.
Example 2
Input
3 1 2 3
Expected
5 2 3
Explanation
Fuel is [1,2,3]. Vehicle 0: ahead=0, behind=2+3=5, diff=5. Vehicle 1: ahead=1, behind=3, diff=2. Vehicle 2: ahead=1+2=3, behind=0, diff=3. Output: 5 2 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →