VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-original-array-from-given-prefix-sum-array/

⇱ Original Array from given Prefix Sums - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Original Array from given Prefix Sums

Last Updated : 18 Mar, 2025

You are given a prefix sum array presum[] of an array arr[]. The task is to find the original arrayarr[] whose prefix sum is presum[].

Examples:

Input:  presum[] = {5, 7, 10, 11, 18}
Output: [5, 2, 3, 1, 7]
Explanation: Original array {5, 2, 3, 1, 7} 
Prefix sum array = {5, 5+2, 5+2+3, 5+2+3+1, 5+2+3+1+7} = {5, 7, 10, 11, 18}
Each element of original array is replaced by the sum of the prefix of current index.

Input: presum[] = {45, 57, 63, 78, 89, 97}
Output: [45, 12, 6, 15, 11, 8] 

The problem can be solved using the following observation:

Given a prefix sum arraypresum[] and the original array arr[] of size n, the prefix sum array is calculated as:

  • presum[0] = arr[0]
  • presum[i] = arr[0] + arr[1] + ... + arr[i] for all i in the range [1, N-1]

This means: presum[i] = presum[i-1] + arr[i]. Hence, the original array elements can be calculated as:

  • arr[0] = presum[0]
  • For all i in the range [1, n-1], arr[i] = presum[i] - presum[i-1]

Steps to Solve:

  1. Traverse the presum[] array starting from the beginning.
  2. If the index i = 0, then arr[i] = presum[i].
  3. Else, for all i > 0, arr[i] = presum[i] - presum[i-1].

Output
45 12 6 15 11 8 

Time Complexity : O(n)
Auxiliary Space : O(1)


Comment