Dada una array arr[] de longitud N, la tarea es encontrar la array original tal que cada i-ésimo elemento en la array dada ( arr[i] ) sea el valor promedio de los primeros i elementos de la array original.
Ejemplos:
Entrada: arr = {4 3 3 3}
Salida: 4 2 3 3
Explicación: (4) / 1 = 1, (4+2) / 2 = 3, (4+2+3) / 3 = 3, (4 +2+3+3) / 4 = 3Entrada: arr = {2 6 8 10}
Salida: 2 10 12 16
Explicación: (2) / 1 = 2, (2+10) / 2 = 6, (2+10+12) / 3 = 8, (2 +10+12+16) / 4 = 10
Enfoque: El problema dado se puede resolver utilizando un enfoque matemático . Siga los pasos a continuación:
- Inicialice una suma variable al primer elemento de la array arr
- Itere la array arr desde el segundo índice hasta el final y en cada iteración:
- Multiplique el elemento actual arr[i] con el índice actual + 1 (i + 1) y réstele el valor de sum
- Agregue el elemento actual resultante a la suma variable
- Devuelva la array resultante después de la modificación, ya que será la array original
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>
2 10 12 16
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por shreyanshgupta838 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA