从给定的前缀和数组中查找原始数组
给定数组的前缀和数组presum[] 。任务是找到前缀和为presum[]的原始数组。
例子:
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]
方法:这个问题可以根据以下观察来解决。
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]
请按照以下步骤解决问题:
- 从数组的开头开始遍历presum[]数组。
- 如果 index (i) = 0则arr[i] = presum[i] 。
- 否则, 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)