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 array arr[]
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 array presum[]
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:
- Traverse the
presum[]
array starting from the beginning. - If the index
i = 0
, then arr[i] = presum[i]
. - Else, for all
i > 0
, arr[i] = presum[i] - presum[i-1]
.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to decode and return the original array as a vector
vector<int> decodeArray(vector<int>& presum) {
int n = presum.size();
vector<int> arr(n);
// Calculating elements of original array
arr[0] = presum[0];
for (int i = 1; i < n; i++) {
arr[i] = presum[i] - presum[i - 1];
}
return arr;
}
int main() {
vector<int> presum = { 45, 57, 63, 78, 89, 97 };
// Function call and storing the result
vector<int> arr = decodeArray(presum);
// Displaying elements of the original array
for (int num : arr) {
cout << num << " ";
}
return 0;
}
Java
// Function to decode and return the original array as a list
import java.util.ArrayList;
import java.util.List;
public class GfG {
public static List<Integer> decodeArray(int[] presum) {
int n = presum.length;
List<Integer> arr = new ArrayList<>(n);
// Calculating elements of original array
arr.add(presum[0]);
for (int i = 1; i < n; i++) {
arr.add(presum[i] - presum[i - 1]);
}
return arr;
}
public static void main(String[] args) {
int[] presum = { 45, 57, 63, 78, 89, 97 };
// Function call and storing the result
List<Integer> arr = decodeArray(presum);
// Displaying elements of the original array
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Python
# Function to decode and return the original array as a list
def decode_array(presum):
# Calculating elements of original array
arr = [presum[0]]
for i in range(1, len(presum)):
arr.append(presum[i] - presum[i - 1])
return arr
if __name__ == '__main__':
presum = [45, 57, 63, 78, 89, 97]
# Function call and storing the result
arr = decode_array(presum)
# Displaying elements of the original array
for num in arr:
print(num, end=' ')
C#
using System;
class GfG {
// Function to decode and return the original
// array as an array
static int[] DecodeArray(int[] presum) {
int n = presum.Length;
int[] arr = new int[n];
// Calculating elements of original array
arr[0] = presum[0];
for (int i = 1; i < n; i++) {
arr[i] = presum[i] - presum[i - 1];
}
return arr;
}
static void Main() {
int[] presum = new int[] { 45, 57, 63, 78, 89, 97 };
// Function call and storing the result
int[] arr = DecodeArray(presum);
// Displaying elements of the original array
foreach (int num in arr) {
Console.Write(num + " ");
}
}
}
JavaScript
// Function to decode and return the original array
function decodeArray(presum) {
const n = presum.length;
let arr = new Array(n);
// Calculating elements of the original array
arr[0] = presum[0];
for (let i = 1; i < n; i++) {
arr[i] = presum[i] - presum[i - 1];
}
return arr;
}
// Driver code to test the function
const presum = [45, 57, 63, 78, 89, 97];
// Function call and storing the result
const arr = decodeArray(presum);
// Displaying elements of the original array
console.log(arr.join(" "));
Time Complexity : O(n)
Auxiliary Space : O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem