Open In App

Find original Array from given Prefix Sum Array

Last Updated : 21 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Traverse the presum[] array starting from the beginning of the array.
  2. If index (i) = 0 then arr[i] = presum[i].
  3. Else, arr[i] = presum[i] – presum[i-1].

Below is the code for the above implementation: 

C++




// C++ implementation for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Finds and prints the elements of
// the original array
void DecodeOriginalArray(int presum[], int N)
{
    // Calculating elements of original array
    for (int i = N - 1; i > 0; i--)
        presum[i] = presum[i] - presum[i - 1];
 
    // Displaying elements of original array
    for (int i = 0; i < N; i++)
        cout << presum[i] << " ";
}
 
// Driver program to test above
int main()
{
    int presum[] = { 45, 57, 63, 78, 89, 97 };
    int N = sizeof(presum) / sizeof(presum[0]);
 
    // Function Call
    DecodeOriginalArray(presum, N);
    return 0;
}


Java




// Java implementation for the above approach
class GFG {
 
  // Finds and prints the elements of
  // the original array
  static void DecodeOriginalArray(int presum[], int N)
  {
 
    // Calculating elements of original array
    for (int i = N - 1; i > 0; i--)
      presum[i] = presum[i] - presum[i - 1];
 
    // Displaying elements of original array
    for (int i = 0; i < N; i++)
      System.out.print(presum[i] + " ");
  }
 
  // Driver program to test above
  public static void main(String args[])
  {
    int presum[] = { 45, 57, 63, 78, 89, 97 };
    int N = presum.length;
 
    // Function Call
    DecodeOriginalArray(presum, N);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3




# Python code for the above approach
 
# Finds and prints the elements of
# the original array
def DecodeOriginalArray(presum, N):
 
    # Calculating elements of original array
    for i in range(N - 1, 0, -1):
        presum[i] = presum[i] - presum[i - 1];
 
    # Displaying elements of original array
    for i in range(N):
        print(presum[i], end= " ");
 
# Driver program to test above
presum = [45, 57, 63, 78, 89, 97];
N = len(presum)
 
# Function Call
DecodeOriginalArray(presum, N);
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Finds and prints the elements of
  // the original array
  static void DecodeOriginalArray(int[ ] presum, int N)
  {
 
    // Calculating elements of original array
    for (int i = N - 1; i > 0; i--)
      presum[i] = presum[i] - presum[i - 1];
 
    // Displaying elements of original array
    for (int i = 0; i < N; i++)
      Console.WriteLine(presum[i]);
  }
 
  // Driver code
  public static void Main(string[] args)
  {
 
    int[] presum = { 45, 57, 63, 78, 89, 97 };
    int N = presum.Length;
 
    // Function Call
    DecodeOriginalArray(presum, N);
  }
}
 
// This code is contributed by hrithikgarg03188


Javascript




<script>
        // JavaScript code for the above approach
 
        // Finds and prints the elements of
        // the original array
        function DecodeOriginalArray(presum, N)
        {
         
            // Calculating elements of original array
            for (let i = N - 1; i > 0; i--)
                presum[i] = presum[i] - presum[i - 1];
 
            // Displaying elements of original array
            for (let i = 0; i < N; i++)
                document.write(presum[i] + " ");
        }
 
        // Driver program to test above
        let presum = [45, 57, 63, 78, 89, 97];
        let N = presum.length;
 
        // Function Call
        DecodeOriginalArray(presum, N);
 
  // This code is contributed by Potta Lokesh
    </script>


Output

45 12 6 15 11 8 

Time complexity: O(N)
Auxiliary Space: O(1) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads