Find original Array from given Prefix Sum Array
Last Updated :
21 Jan, 2022
Given prefix sum array presum[] of an array. The task is to find the original array 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]
Approach: This problem can be solved based on the following observation.
Given prefix sum array presum[] and suppose the original array is arr[] and the size is N.
The presum[] array is calculated as follows:
- presum[0] = arr[0]
- presum[i] = arr[0] + arr[1] + . . . + arr[i] for all i in range [1, N-1]
So, presum[i] = arr[0] + arr[i] + . . . + arr[i-1] + arr[i]
= presum[i-1] + arr[i]
Therefore, arr[i] = presum[i] – presum[i-1]. for all i in range [1, N-1] and,
arr[0] = presum[0]
Follow the steps mentioned below to solve the problem:
- Traverse the presum[] array starting from the beginning of the array.
- If index (i) = 0 then arr[i] = presum[i].
- Else, arr[i] = presum[i] – presum[i-1].
Below is the code for the above implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void DecodeOriginalArray( int presum[], int N)
{
for ( int i = N - 1; i > 0; i--)
presum[i] = presum[i] - presum[i - 1];
for ( int i = 0; i < N; i++)
cout << presum[i] << " " ;
}
int main()
{
int presum[] = { 45, 57, 63, 78, 89, 97 };
int N = sizeof (presum) / sizeof (presum[0]);
DecodeOriginalArray(presum, N);
return 0;
}
|
Java
class GFG {
static void DecodeOriginalArray( int presum[], int N)
{
for ( int i = N - 1 ; i > 0 ; i--)
presum[i] = presum[i] - presum[i - 1 ];
for ( int i = 0 ; i < N; i++)
System.out.print(presum[i] + " " );
}
public static void main(String args[])
{
int presum[] = { 45 , 57 , 63 , 78 , 89 , 97 };
int N = presum.length;
DecodeOriginalArray(presum, N);
}
}
|
Python3
def DecodeOriginalArray(presum, N):
for i in range (N - 1 , 0 , - 1 ):
presum[i] = presum[i] - presum[i - 1 ];
for i in range (N):
print (presum[i], end = " " );
presum = [ 45 , 57 , 63 , 78 , 89 , 97 ];
N = len (presum)
DecodeOriginalArray(presum, N);
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void DecodeOriginalArray( int [ ] presum, int N)
{
for ( int i = N - 1; i > 0; i--)
presum[i] = presum[i] - presum[i - 1];
for ( int i = 0; i < N; i++)
Console.WriteLine(presum[i]);
}
public static void Main( string [] args)
{
int [] presum = { 45, 57, 63, 78, 89, 97 };
int N = presum.Length;
DecodeOriginalArray(presum, N);
}
}
|
Javascript
<script>
function DecodeOriginalArray(presum, N)
{
for (let i = N - 1; i > 0; i--)
presum[i] = presum[i] - presum[i - 1];
for (let i = 0; i < N; i++)
document.write(presum[i] + " " );
}
let presum = [45, 57, 63, 78, 89, 97];
let N = presum.length;
DecodeOriginalArray(presum, N);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...