Reconstruct Alternating Sequence — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(n)

Quick Answer & Algorithm Key Takeaway

Master coding challenges related to Arrays and solve the Reconstruct Alternating Sequence problem optimally.

TopicArrays
PatternMixed
TimeO(n)
SpaceO(n)

Problem Description

Given an integer array diff, reconstruct the original sequence a. The sequence starts with a[0]=0. For each index i (0‑based) in diff, if i is even, set a[i+1]=a[i]+diff[i]; otherwise set a[i+1]=a[i]-diff[i]. Return the full array a of length diff.length+1.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Reconstruct Alternating Sequence"

medium

WHY DOES IT MATTER?

The pattern is a deterministic prefix‑sum with alternating signs, a micro‑cosm of many real‑world cumulative calculations (e.g., net cash flow, signal processing). Mastering it builds intuition for transforming iterative definitions into O(1) updates.

OPTIMIZATION CHALLENGE

The insight is that the sign for each diff[i] is known ahead of time based solely on index parity, so we can fold the sign into the iteration and avoid recomputing sums from scratch, collapsing O(n²) work to O(n).

REAL-WORLD CONNECTION

Think of a bank ledger where deposits and withdrawals alternate each day; the balance after each day is the previous balance plus or minus the day's transaction. Computing the full balance history efficiently mirrors this algorithm.

During an interview, write the loop first, explicitly handling the parity check, and immediately return the built array; avoid over‑engineering with extra data structures—simplicity wins.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(n)

Core Theory — Why This Approach?

The reconstruction task is a classic prefix‑sum problem where each element of the target array a is derived from the previous element and a signed contribution from diff. By iterating once over diff and applying +diff[i] on even indices and -diff[i] on odd indices, we accumulate a running total that directly yields a[i+1]. A naïve alternative would be to recompute each a[k] from scratch by summing the appropriate signed diff values, leading to O(n²) time for n=diff.length, which quickly becomes infeasible for large inputs (e.g., n≈10⁶). The optimal paradigm leverages the linearity of addition and the deterministic sign pattern, allowing a single pass that updates the current prefix sum in O(1) per element, achieving overall O(n) time and O(1) auxiliary space. This approach exemplifies how recognizing a problem as a cumulative‑sum or “running total” scenario can collapse quadratic work into linear work, a recurring theme in array‑based interview problems.

Interview Questions on This Problem

Q1How would you modify the algorithm if the sign rule were reversed (odd indices add, even indices subtract) while still maintaining O(n) time?

Swap the parity check: for each i, if i%2==0 subtract diff[i] else add diff[i]; the rest of the linear scan remains unchanged, preserving O(n) time and O(1) extra space.

Q2At a fintech firm, you need to reconstruct a transaction balance series where diff may contain up to 10⁹ values and could overflow 32‑bit integers. How do you safeguard your solution?

Use a 64‑bit integer type (e.g., long long in C++, long in Java, or Python's arbitrary‑precision int) for the running sum and the result array to avoid overflow, while the algorithmic complexity stays O(n).

Q3A startup asks you to return the sequence modulo 1 000 000 007 because the numbers are huge. How does this affect the algorithm?

Apply the modulo operation after each addition or subtraction (taking care to keep the result non‑negative) during the single pass; the core O(n) logic is unchanged, only the arithmetic is performed modulo the given constant.

Examples

Example 1

Input

[5,2,4]

Output

[0,5,3,7]

Explanation: Start with 0. i=0 (even) add 5 →5. i=1 (odd) subtract 2 →3. i=2 (even) add 4 →7. The resulting sequence is [0,5,3,7].

Example 2

Input

[1,1,1,1]

Output

[0,1,0,1,0]

Explanation: 0 → +1 =1 → -1 =0 → +1 =1 → -1 =0, giving [0,1,0,1,0].

Example 3

Input

[10,20,30,40]

Output

[0,10,-10,20,-20]

Explanation: 0 → +10 =10 → -20 =-10 → +30 =20 → -40 =-20, resulting in [0,10,-10,20,-20].

Constraints

  • 1 <= diff.length <= 100000
  • -1000000000 <= diff[i] <= 1000000000
  • All intermediate and final values fit in 64‑bit signed integer

Optimal Approach & Strategy

Maintain a running sum while scanning diff once, applying + or – based on index parity, and append each new sum to the result array.

Brute Force Approach

For each position j compute a[j] by summing all signed diff[0..j‑1] from scratch, leading to a nested loop.

Code Solutions

JavaScript Solution
Time: O(n)
const fs = require('fs');
const data = fs.readFileSync(0, 'utf8').trim().split(/\s+/).map(Number);
let pos = 0;
function reconstructSequence(diff) {
    const a = new Array(diff.length + 1);
    a[0] = 0;
    for (let i = 0; i < diff.length; ++i) {
        if (i % 2 === 0) a[i + 1] = a[i] + diff[i];
        else a[i + 1] = a[i] - diff[i];
    }
    return a;
}
if (data.length === 0) process.exit();
const n = data[pos++];
const diff = data.slice(pos, pos + n);
const ans = reconstructSequence(diff);
console.log(ans.join(' '));

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.