BackmediumArraysPaytm

Reconstruct Alternating Sequence Solution

Problem Statement

Given an array of integers diff 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.

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

Explanation: Step 1: Initialize the result array with the first element as 0. Result = [0]. Step 2: Iterate through the differences array. For each difference, if the index is even, add the difference to the last element of the result array. If the index is odd, subtract the difference from the last element of the result array. Result = [0, 1, -2, 3, -4, 5].

Example 2
Input
[5, 2, 3]
Output
[0, 5, 2, 3, -7]

Explanation: Step 1: Initialize the result array with the first element as 0. Result = [0]. Step 2: Iterate through the differences array. For each difference, if the index is even, add the difference to the last element of the result array. If the index is odd, subtract the difference from the last element of the result array. Result = [0, 5, 2, 3, -7].

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 Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Reconstruct Alternating Sequence — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

Given an array of integers diff 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.

Examples

Example 1

Input

[1, -2, 3, -4]

Output

[0, 1, -2, 3, -4, 5]

Explanation: Step 1: Initialize the result array with the first element as 0. Result = [0]. Step 2: Iterate through the differences array. For each difference, if the index is even, add the difference to the last element of the result array. If the index is odd, subtract the difference from the last element of the result array. Result = [0, 1, -2, 3, -4, 5].

Example 2

Input

[5, 2, 3]

Output

[0, 5, 2, 3, -7]

Explanation: Step 1: Initialize the result array with the first element as 0. Result = [0]. Step 2: Iterate through the differences array. For each difference, if the index is even, add the difference to the last element of the result array. If the index is odd, subtract the difference from the last element of the result array. Result = [0, 5, 2, 3, -7].

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.

Optimal Approach & Strategy

The optimal approach involves initializing the result array with 0 and then iteratively applying the given differences, switching between addition and subtraction based on the pattern. This results in a linear time complexity.

Brute Force Approach

One naive approach could involve trying all possible initial numbers and then iteratively adjusting them based on given differences, but this would lead to an inefficient O(n²) complexity. This method involves significant repeated computation. It's clearly not suitable for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function reconstructAlternatingSequence(diff) {
  let result = [0];
  for (let i = 0; i < diff.length; i++) {
    result.push(result[result.length - 1] + (i % 2 === 0 ? diff[i] : -diff[i]));
  }
  return result;
}

Asked in Top Tech Interviews

Paytm

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.