Find the original Array from given array where ith element is the average of first i elements Last Updated : 26 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array arr[] of length N, the task is to find the original array such that every ith element in the given array(arr[i]) is the average value of the first i elements of the original array. Examples: Input: arr = {4 3 3 3} Output: 4 2 3 3Explanation: (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 16Explanation: (2) / 1 = 2, (2+10) / 2 = 6, (2+10+12) / 3 = 8, (2+10+12+16) / 4 = 10 Approach: The given problem can be solved using a mathematical approach. Follow the steps below: Initialize a variable sum to the first element of the array arrIterate the array arr from 2nd index till the end and at every iteration:Multiply current element arr[i] with current index + 1 (i + 1) and subtract the value of sum from itAdd the resulting current element to the variable sumReturn the resulting array after modification as it will be the original array C++ // C++ implementation for the above approach #include <iostream> 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 <script> // JavaScript implementation for the above approach // Function to find the original // array from the modified array function findOriginal(arr, N) { // Initialize the variable sum // with the first element of array var sum = arr[0]; for (var 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 (var i = 0; i < N; i++) { document.write(arr[i] + " "); } } // Driver code var arr = [ 2, 6, 8, 10 ]; var N = arr.length; // Call the function findOriginal(arr, N); // This code is contributed by AnkThon </script> Output2 10 12 16 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Average of remaining elements after removing K largest and K smallest elements from array S shreyanshgupta838 Follow Improve Article Tags : Greedy Mathematical DSA Arrays Practice Tags : ArraysGreedyMathematical Similar Reads Find the deleted value from the array when average of original elements is given Given an array of length N + K. Also given the average avg of all the elements of the array. If an element that appears exactly K time got removed from the array (all the occurrences) and the resultant array is given, the task is to find the element X. Note that if X is not an integer then print -1. 5 min read Replace all elements of given Array with average of previous K and next K elements Given an array arr[] containing N positive integers and an integer K. The task is to replace every array element with the average of previous K and next K elements. Also, if K elements are not present then adjust use the maximum number of elements available before and after. Examples: Input: arr[] = 11 min read Average of remaining elements after removing K largest and K smallest elements from array Given an array of N integers. The task is to find the average of the numbers after removing k largest elements and k smallest element from the array i.e. calculate the average value of the remaining N - 2K elements. Examples: Input: arr = [1, 2, 4, 4, 5, 6], K = 2Output: 4Remove 2 smallest elements 5 min read Maximize the first element of the array such that average remains constant Given an array arr[] of length N and an integer X, the task is to find the maximum value R of updated first element such that the average of the array remains constant and no elements of the array should become negative. The maximum value of the first element should be in the range arr[0] <= R 6 min read Count occurrences of the average of array elements with a given number Given an array of N integers and an integer x . For every integer of the array a[i], the task is to calculate the count of numbers in the array with value equals to the average of element a[i] and x. That is, the number of occurrences of the (average of element a[i] and x) in the array. Examples: In 7 min read Count occurrences of the average of array elements with a given number Given an array of N integers and an integer x . For every integer of the array a[i], the task is to calculate the count of numbers in the array with value equals to the average of element a[i] and x. That is, the number of occurrences of the (average of element a[i] and x) in the array. Examples: In 7 min read Like