📌  相关文章
📜  从给定的前缀和数组中查找原始数组

📅  最后修改于: 2022-05-13 01:56:09.329000             🧑  作者: Mango

从给定的前缀和数组中查找原始数组

给定数组的前缀和数组presum[] 。任务是找到前缀和为presum[]的原始数组。

例子:

方法:这个问题可以根据以下观察来解决。

请按照以下步骤解决问题:

  1. 从数组的开头开始遍历presum[]数组。
  2. 如果 index (i) = 0arr[i] = presum[i]
  3. 否则, arr[i] = presum[i] – presum[i-1]

下面是上述实现的代码:

C++
// C++ implementation for the above approach
#include 
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


输出
45 12 6 15 11 8 

时间复杂度: 在)
辅助空间: O(1)