Dada una array arr[] de tamaño N y una array Q[][] en la que cada fila representa una consulta de la forma { X, Y } , la tarea de cada consulta es encontrar la suma de los elementos de la array presentes en los índices X , X + Y , X + 2 * Y + …
Ejemplos:
Entrada: arr[] = { 1, 2, 7, 5, 4 }, Q[][] = { { 2, 1 }, { 3, 2 } }
Salida: 16 5
Explicación:
Consulta1: arr[2] + arr[2 + 1] + arr[2 + 2] = 7 + 5 + 4 = 16.
Consulta2: arr[3] = 5.Entrada: arr[] = { 3, 6, 1, 8, 0 } Q[][] = { { 0, 2 } }
Salida: 4
Enfoque ingenuo: el enfoque más simple para resolver este problema es recorrer la array para cada consulta e imprimir la suma de arr[x] + arr[x + y] + arr[x + 2 * y] + …
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries void querySum(int arr[], int N, int Q[][2], int M) { // Iterate over each query for (int i = 0; i < M; i++) { int x = Q[i][0]; int y = Q[i][1]; // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int sum = 0; // Traverse the array and calculate // the sum of the expression while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } cout << sum << " "; } } // Driver Code int main() { int arr[] = { 1, 2, 7, 5, 4 }; int Q[][2] = { { 2, 1 }, { 3, 2 } }; int N = sizeof(arr) / sizeof(arr[0]); int M = sizeof(Q) / sizeof(Q[0]); querySum(arr, N, Q, M); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries static void querySum(int arr[], int N, int Q[][], int M) { // Iterate over each query for(int i = 0; i < M; i++) { int x = Q[i][0]; int y = Q[i][1]; // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int sum = 0; // Traverse the array and calculate // the sum of the expression while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } System.out.print(sum + " "); } } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 7, 5, 4 }; int Q[][] = { { 2, 1 }, { 3, 2 } }; int N = arr.length; int M = Q.length; querySum(arr, N, Q, M); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach # Function to Find the sum of # arr[x]+arr[x+y]+arr[x+2*y] + ... # for all queries def querySum(arr, N, Q, M): # Iterate over each query for i in range(M): x = Q[i][0] y = Q[i][1] # Stores the sum of # arr[x]+arr[x+y]+arr[x+2*y] + ... sum = 0 # Traverse the array and calculate # the sum of the expression while (x < N): # Update sum sum += arr[x] # Update x x += y print(sum, end=" ") # Driver Code if __name__ == '__main__': arr = [ 1, 2, 7, 5, 4 ]; Q = [ [ 2, 1 ], [3, 2 ] ] N = len(arr) M = len(Q) querySum(arr, N, Q, M) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; class GFG { // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries static void querySum(int []arr, int N, int [,]Q, int M) { // Iterate over each query for(int i = 0; i < M; i++) { int x = Q[i, 0]; int y = Q[i, 1]; // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int sum = 0; // Traverse the array and calculate // the sum of the expression while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } Console.Write(sum + " "); } } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 7, 5, 4 }; int [,]Q = { { 2, 1 }, { 3, 2 } }; int N = arr.Length; int M = Q.GetLength(0); querySum(arr, N, Q, M); } } // This code is contributed by shikhasingrajput
Javascript
<script> // JavaScript program for the above approach // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries function querySum(arr, N, Q, M) { // Iterate over each query for (let i = 0; i < M; i++) { let x = Q[i][0]; let y = Q[i][1]; // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... let sum = 0; // Traverse the array and calculate // the sum of the expression while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } document.write(sum + " "); } } // Driver Code let arr = [ 1, 2, 7, 5, 4 ]; let Q = [ [ 2, 1 ], [ 3, 2 ] ]; let N = arr.length; let M = Q.length; querySum(arr, N, Q, M); // This code is contributed by Surbhi Tyagi. </script>
16 5
Complejidad de Tiempo: O(|Q| * O(N))
Espacio Auxiliar: O(1)
Enfoque eficiente: el problema se puede resolver calculando previamente el valor de la expresión dada para todos los valores posibles de {X, Y} utilizando la técnica de programación dinámica y la técnica de descomposición de raíz cuadrada . Las siguientes son las relaciones de recurrencia:
si i + j < N
dp[i][j] = dp[i + j][j] + arr[i]
De lo contrario,
dp[i][j] = arr[i]dp[i][j]: Almacena la suma de la expresión dada donde X = i, Y = j
Siga los pasos a continuación para resolver el problema:
- Inicialice una array 2D , digamos dp[][] , para almacenar la suma de la expresión para todos los valores posibles de X e Y , donde Y es menor o igual que sqrt(N) .
- Llene la array dp[][] usando el método de tabulación .
- Recorre la array Q[][] . Para cada consulta, verifique si el valor de Q[i][1] es menor o igual a sqrt(N) o no. Si se encuentra que es cierto, imprima el valor de dp[Q[i][0]][Q[i][1]] .
- De lo contrario, calcule el valor de la expresión utilizando el enfoque ingenuo anterior e imprima el valor calculado.
A continuación se muestra la implementación de nuestro enfoque:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; const int sz = 20; const int sqr = int(sqrt(sz)) + 1; // Function to sum of arr[x]+arr[x+y]+arr[x+2*y] + ... // for all possible values of X and Y, where Y is // less than or equal to sqrt(N). void precomputeExpressionForAllVal(int arr[], int N, int dp[sz][sqr]) { // Iterate over all possible values of X for (int i = N - 1; i >= 0; i--) { // Precompute for all possible values // of an expression such that y <= sqrt(N) for (int j = 1; j <= sqrt(N); j++) { // If i + j less than N if (i + j < N) { // Update dp[i][j] dp[i][j] = arr[i] + dp[i + j][j]; } else { // Update dp[i][j] dp[i][j] = arr[i]; } } } } // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries int querySum(int arr[], int N, int Q[][2], int M) { // dp[x][y]: Stores sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int dp[sz][sqr]; precomputeExpressionForAllVal(arr, N, dp); // Traverse the query array, Q[][] for (int i = 0; i < M; i++) { int x = Q[i][0]; int y = Q[i][1]; // If y is less than or equal // to sqrt(N) if (y <= sqrt(N)) { cout << dp[x][y] << " "; continue; } // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int sum = 0; // Traverse the array, arr[] while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } cout << sum << " "; } } // Driver Code int main() { int arr[] = { 1, 2, 7, 5, 4 }; int Q[][2] = { { 2, 1 }, { 3, 2 } }; int N = sizeof(arr) / sizeof(arr[0]); int M = sizeof(Q) / sizeof(Q[0]); querySum(arr, N, Q, M); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ static int sz = 20; static int sqr = (int)(Math.sqrt(sz)) + 1; // Function to sum of arr[x]+arr[x+y]+arr[x+2*y] + ... // for all possible values of X and Y, where Y is // less than or equal to Math.sqrt(N). static void precomputeExpressionForAllVal(int arr[], int N, int dp[][]) { // Iterate over all possible values of X for(int i = N - 1; i >= 0; i--) { // Precompute for all possible values // of an expression such that y <= Math.sqrt(N) for(int j = 1; j <= Math.sqrt(N); j++) { // If i + j less than N if (i + j < N) { // Update dp[i][j] dp[i][j] = arr[i] + dp[i + j][j]; } else { // Update dp[i][j] dp[i][j] = arr[i]; } } } } // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries static void querySum(int arr[], int N, int Q[][], int M) { // dp[x][y]: Stores sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int [][]dp = new int[sz][sqr]; precomputeExpressionForAllVal(arr, N, dp); // Traverse the query array, Q[][] for(int i = 0; i < M; i++) { int x = Q[i][0]; int y = Q[i][1]; // If y is less than or equal // to Math.sqrt(N) if (y <= Math.sqrt(N)) { System.out.print(dp[x][y] + " "); continue; } // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int sum = 0; // Traverse the array, arr[] while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } System.out.print(sum + " "); } } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 7, 5, 4 }; int Q[][] = { { 2, 1 }, { 3, 2 } }; int N = arr.length; int M = Q.length; querySum(arr, N, Q, M); } } // This code is contributed by shikhasingrajput
Python3
# python program for the above approach import math sz = 20 sqr = int(math.sqrt(sz)) + 1 # Function to sum of arr[x]+arr[x+y]+arr[x+2*y] + ... # for all possible values of X and Y, where Y is # less than or equal to sqrt(N). def precomputeExpressionForAllVal(arr, N, dp): # Iterate over all possible values of X for i in range(N - 1, -1, -1) : # Precompute for all possible values # of an expression such that y <= sqrt(N) for j in range (1,int(math.sqrt(N)) + 1): # If i + j less than N if (i + j < N): # Update dp[i][j] dp[i][j] = arr[i] + dp[i + j][j] else: # Update dp[i][j] dp[i][j] = arr[i] # Function to Find the sum of # arr[x]+arr[x+y]+arr[x+2*y] + ... # for all queries def querySum(arr, N, Q, M): # dp[x][y]: Stores sum of # arr[x]+arr[x+y]+arr[x+2*y] + ... dp = [ [0 for x in range(sz)]for x in range(sqr)] precomputeExpressionForAllVal(arr, N, dp) # Traverse the query array, Q[][] for i in range (0,M): x = Q[i][0] y = Q[i][1] # If y is less than or equal # to sqrt(N) if (y <= math.sqrt(N)): print(dp[x][y]) continue # Stores the sum of # arr[x]+arr[x+y]+arr[x+2*y] + ... sum = 0 # Traverse the array, arr[] while (x < N): # Update sum sum += arr[x] # Update x x += y print(sum) # Driver Code arr = [ 1, 2, 7, 5, 4 ] Q = [ [ 2, 1 ], [ 3, 2]] N = len(arr) M = len(Q[0]) querySum(arr, N, Q, M) # This code is contributed by amreshkumar3.
C#
// C# program for the above approach using System; class GFG{ static int sz = 20; static int sqr = (int)(Math.Sqrt(sz)) + 1; // Function to sum of arr[x]+arr[x+y]+arr[x+2*y] + ... // for all possible values of X and Y, where Y is // less than or equal to Math.Sqrt(N). static void precomputeExpressionForAllVal(int []arr, int N, int [,]dp) { // Iterate over all possible values of X for(int i = N - 1; i >= 0; i--) { // Precompute for all possible values // of an expression such that y <= Math.Sqrt(N) for(int j = 1; j <= Math.Sqrt(N); j++) { // If i + j less than N if (i + j < N) { // Update dp[i,j] dp[i, j] = arr[i] + dp[i + j, j]; } else { // Update dp[i,j] dp[i, j] = arr[i]; } } } } // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries static void querySum(int []arr, int N, int [,]Q, int M) { // dp[x,y]: Stores sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int [,]dp = new int[sz, sqr]; precomputeExpressionForAllVal(arr, N, dp); // Traverse the query array, Q[,] for(int i = 0; i < M; i++) { int x = Q[i, 0]; int y = Q[i, 1]; // If y is less than or equal // to Math.Sqrt(N) if (y <= Math.Sqrt(N)) { Console.Write(dp[x, y] + " "); continue; } // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... int sum = 0; // Traverse the array, []arr while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } Console.Write(sum + " "); } } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 7, 5, 4 }; int [,]Q = { { 2, 1 }, { 3, 2 } }; int N = arr.Length; int M = Q.GetLength(0); querySum(arr, N, Q, M); } } // This code is contributed by shikhasingrajput
Javascript
<script> // javascript program of the above approach let sz = 20; let sqr = (Math.sqrt(sz)) + 1; // Function to sum of arr[x]+arr[x+y]+arr[x+2*y] + ... // for all possible values of X and Y, where Y is // less than or equal to Math.sqrt(N). function precomputeExpressionForAllVal(arr, N, dp) { // Iterate over all possible values of X for(let i = N - 1; i >= 0; i--) { // Precompute for all possible values // of an expression such that y <= Math.sqrt(N) for(let j = 1; j <= Math.sqrt(N); j++) { // If i + j less than N if (i + j < N) { // Update dp[i][j] dp[i][j] = arr[i] + dp[i + j][j]; } else { // Update dp[i][j] dp[i][j] = arr[i]; } } } } // Function to Find the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... // for all queries function querySum(arr, N, Q, M) { let dp = new Array(sz); // Loop to create 2D array using 1D array for (var i = 0; i < dp.length; i++) { dp[i] = new Array(2); } // Loop to create 2D array using 1D array for (var i = 0; i < dp.length; i++) { dp[i] = new Array(2); } precomputeExpressionForAllVal(arr, N, dp); // Traverse the query array, Q[][] for(let i = 0; i < M; i++) { let x = Q[i][0]; let y = Q[i][1]; // If y is less than or equal // to Math.sqrt(N) if (y <= Math.sqrt(N)) { document.write(dp[x][y] + " "); continue; } // Stores the sum of // arr[x]+arr[x+y]+arr[x+2*y] + ... let sum = 0; // Traverse the array, arr[] while (x < N) { // Update sum sum += arr[x]; // Update x x += y; } Sdocument.write(sum + " "); } } // Driver Code // Given array let arr = [ 1, 2, 7, 5, 4 ]; let Q= [[ 2, 1 ], [ 3, 2 ]] let N = arr.length; let M = Q.length; querySum(arr, N, Q, M); // This code is contributed by chinmoy1997pal. </script>
16 5
Complejidad de tiempo: O(N * sqrt(N) + |Q| * sqrt(N))
Espacio auxiliar: O(N * sqrt(N))
Publicación traducida automáticamente
Artículo escrito por lostsoul27 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA