从给定数组中找到原始数组,其中第 i 个元素是前 i 个元素的平均值
给定一个数组arr[] 长度为N,任务是找到原始数组,使得给定数组 ( arr[i] ) 中的每个第i 个元素都是原始数组的前i个元素的平均值。
例子:
Input: arr = {4 3 3 3}
Output: 4 2 3 3
Explanation: (4) / 1 = 1, (4+2) / 2 = 3, (4+2+3) / 3 = 3, (4+2+3+3) / 4 = 3
Input: arr = {2 6 8 10}
Output: 2 10 12 16
Explanation: (2) / 1 = 2, (2+10) / 2 = 6, (2+10+12) / 3 = 8, (2+10+12+16) / 4 = 10
方法:给定的问题可以使用数学方法来解决。请按照以下步骤操作:
- 将变量sum初始化为数组arr的第一个元素
- 迭代数组arr从第 2 个索引到末尾,并在每次迭代时:
- 将当前元素arr[i]与当前索引 + 1 (i + 1)相乘并从中减去sum的值
- 将生成的当前元素添加到变量sum
- 修改后返回结果数组,因为它将是原始数组
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to find the original
// array from the modified array
void findOriginal(int arr[], int N)
{
// Initialize the variable sum
// with the first element of array
int sum = arr[0];
for (int i = 1; i < N; i++) {
// Calculate original element
// from average of first i elements
arr[i] = (i + 1) * arr[i] - sum;
// Add current element to sum
sum += arr[i];
}
// Print the array
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver function
int main()
{
int arr[] = { 2, 6, 8, 10 };
int N = sizeof(arr) / sizeof(arr[0]);
// Call the function
findOriginal(arr, N);
return 0;
}
Java
// Java implementation for the above approach
class GFG
{
// Function to find the original
// array from the modified array
static void findOriginal(int arr[], int N)
{
// Initialize the variable sum
// with the first element of array
int sum = arr[0];
for (int i = 1; i < N; i++) {
// Calculate original element
// from average of first i elements
arr[i] = (i + 1) * arr[i] - sum;
// Add current element to sum
sum += arr[i];
}
// Print the array
for (int i = 0; i < N; i++) {
System.out.print(arr[i] + " ");
}
}
// Driver function
public static void main(String [] args)
{
int [] arr = new int [] { 2, 6, 8, 10 };
int N = arr.length;
// Call the function
findOriginal(arr, N);
}
}
// This code is contributed by ihritik
Python3
# Python implementation for the above approach
# Function to find the original
# array from the modified array
def findOriginal(arr, N):
# Initialize the variable sum
# with the first element of array
sum = arr[0]
for i in range(1, N):
# Calculate original element
# from average of first i elements
arr[i] = (i + 1) * arr[i] - sum
# Add current element to sum
sum = sum + arr[i]
# Print the array
for i in range (0, N):
print(arr[i], end=" ")
# Driver function
arr= [ 2, 6, 8, 10 ]
N = len(arr)
# Call the function
findOriginal(arr, N)
# This code is contributed by ihritik
C#
// C# program for above approach
using System;
class GFG {
// Function to find the original
// array from the modified array
static void findOriginal(int[] arr, int N)
{
// Initialize the variable sum
// with the first element of array
int sum = arr[0];
for (int i = 1; i < N; i++) {
// Calculate original element
// from average of first i elements
arr[i] = (i + 1) * arr[i] - sum;
// Add current element to sum
sum += arr[i];
}
// Print the array
for (int i = 0; i < N; i++)
Console.Write(arr[i] + " ");
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
int[] arr = { 2, 6, 8, 10 };
findOriginal(arr, N);
}
}
// This code is contributed by dwivediyash
Javascript
输出
2 10 12 16
时间复杂度: O(N)
辅助空间: O(1)