BackmediumArrays Oracle

Alternating Array Reconstruction Solution

Problem Statement

Given an array of integers representing the differences between consecutive elements in a reconstructed array, determine the original array that these differences represent, assuming the first element is 0 and differences alternate between being added and subtracted from the running total. The length of the output array is equal to the length of the input array plus one.

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

Explanation: Step-by-step: Given the input array [1, -2, 3, -4], we initialize the output array with the first element as 0. Then, we iterate over the input array. For each element, we add it to the running total if the index is even and subtract it if the index is odd. This gives us the output array [0, 1, -2, 3, -4].

Example 2
Input
[5, -10]
Output
[0, 5, -10]

Explanation: Step-by-step: Given the input array [5, -10], we initialize the output array with the first element as 0. Then, we iterate over the input array. For each element, we add it to the running total if the index is even and subtract it if the index is odd. This gives us the output array [0, 5, -10].

Constraints

  • The length of the differences array will not exceed 10^5 elements.
  • All numbers in the differences array are integers between -10^9 and 10^9.
  • The input array may be empty, in which case the output is [0].
  • The total sum of absolute values of differences will not exceed 10^6.
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