Dada una array arr[] de N enteros y un número K , la tarea es encontrar la suma de la diferencia absoluta de todos los pares elevados a la potencia K en una array dada, es decir, .
Ejemplos:
Entrada: arr[] = {1, 2, 3}, K = 1
Salida: 8
Explicación:
Suma de |1-1|+|1-2|+|1-3|+|2-1|+|2 -2|+|2-3|+|3-1|+|3-2|+|3-3| = 8
Entrada: arr[] = {1, 2, 3}, K = 3
Salida: 20
Explicación:
Suma de |1 – 1| 3 + |1 – 2| 3 + |1 – 3| 3 + |2 – 1| 3 + |2 – 2| 3 + |2 – 3| 3 + |3 – 1| 3 + |3 – 2| 3 + |3 – 3| 3 = 20
Enfoque ingenuo: la idea es generar todos los pares posibles y encontrar la diferencia absoluta de cada par elevado a la potencia K y sumarlos.
Complejidad de tiempo: O((log K)*N 2 )
Espacio auxiliar: O(1)
Enfoque eficiente: Podemos mejorar la complejidad de tiempo del enfoque ingenuo con los siguientes cálculos:
Para todos los pares posibles, tenemos que encontrar el valor de
Dado que para pares (arr[i], arr[j]) el valor de se calcula dos veces. Entonces la ecuación anterior también se puede escribir como:
Escribiendo en términos de ecuación binomial:
(Ecuación 1)
Sea Pre[i][a] = (Ecuación 2)
De la Ecuación 1 y la Ecuación 2, tenemos
El valor de Pre[i][a] se puede calcular como:
Pre[i][a] = {(-arr[1]) K – a + (-arr[2]) K – a … . . +(-arr[i – 1]) K – a }.
Entonces, Pre[i+1][a] = Pre[i][a]+(-arr[i]) K – a
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> #define ll long long using namespace std; class Solution { public: // Since K can be 100 max ll ncr[101][101]; int n, k; vector<ll> A; // Constructor Solution(int N, int K, vector<ll> B) { // Initializing with -1 memset(ncr, -1, sizeof(ncr)); n = N; k = K; A = B; // Making vector A as 1-Indexing A.insert(A.begin(), 0); } ll f(int N, int K); ll pairsPower(); }; // To Calculate the value nCk ll Solution::f(int n, int k) { if (k == 0) return 1LL; if (n == k) return 1LL; if (n < k) return 0; if (ncr[n][k] != -1) return ncr[n][k]; // Since nCj = (n-1)Cj + (n-1)C(j-1); return ncr[n][k] = f(n - 1, k) + f(n - 1, k - 1); } // Function that summation of absolute // differences of all pairs raised // to the power k ll Solution::pairsPower() { ll pre[n + 1][k + 1]; ll ans = 0; // Sort the given array sort(A.begin() + 1, A.end()); // Precomputation part, O(n*k) for (int i = 1; i <= n; ++i) { pre[i][0] = 1LL; for (int j = 1; j <= k; j++) { pre[i][j] = A[i] * pre[i][j - 1]; } if (i != 1) { for (int j = 0; j <= k; ++j) pre[i][j] = pre[i][j] + pre[i - 1][j]; } } // Traverse the array arr[] for (int i = n; i >= 2; --i) { // For each K for (int j = 0; j <= k; j++) { ll val = f(k, j); ll val1 = pow(A[i], k - j) * pre[i - 1][j]; val = val * val1; if (j % 2 == 0) ans = (ans + val); else ans = (ans - val); } } ans = 2LL * ans; // Return the final answer return ans; } // Driver Code int main() { // Given N and K int N = 3; int K = 3; // Given array vector<ll> arr = { 1, 2, 3 }; // Creation of Object of class Solution obj(N, K, arr); // Function Call cout << obj.pairsPower() << endl; return 0; }
Python3
# Python3 program for the above approach class Solution: def __init__(self, N, K, B): self.ncr = [] # Since K can be 100 max for i in range(101): temp = [] for j in range(101): # Initializing with -1 temp.append(-1) self.ncr.append(temp) self.n = N self.k = K # Making vector A as 1-Indexing self.A = [0] + B # To Calculate the value nCk def f(self, n, k): if k == 0: return 1 if n == k: return 1 if n < k: return 0 if self.ncr[n][k] != -1: return self.ncr[n][k] # Since nCj = (n-1)Cj + (n-1)C(j-1); self.ncr[n][k] = (self.f(n - 1, k) + self.f(n - 1, k - 1)) return self.ncr[n][k] # Function that summation of absolute # differences of all pairs raised # to the power k def pairsPower(self): pre = [] for i in range(self.n + 1): temp = [] for j in range(self.k + 1): temp.append(0) pre.append(temp) ans = 0 # Sort the given array self.A.sort() # Precomputation part, O(n*k) for i in range(1, self.n + 1): pre[i][0] = 1 for j in range(1, self.k + 1): pre[i][j] = (self.A[i] * pre[i][j - 1]) if i != 1: for j in range(self.k + 1): pre[i][j] = (pre[i][j] + pre[i - 1][j]) # Traverse the array arr[] for i in range(self.n, 1, -1): # For each K for j in range(self.k + 1): val = self.f(self.k, j) val1 = (pow(self.A[i], self.k - j) * pre[i - 1][j]) val = val * val1 if j % 2 == 0: ans = ans + val else: ans = ans - val ans = 2 * ans # Return the final answer return ans # Driver code if __name__ == '__main__': # Given N and K N = 3 K = 3 # Given array arr = [ 1, 2, 3 ] # Creation of object of class obj = Solution(N, K, arr) # Function call print(obj.pairsPower()) # This code is contributed by Shivam Singh
C#
// C# program to implement above approach using System; using System.Collections; using System.Collections.Generic; class GFG { // Since K can be 100 max static long[][] ncr = new long[101][]; static int n, k; static List<long> A; // To Calculate the value nCk static long f(int n, int k) { if (k == 0) return (long)1; if (n == k) return (long)1; if (n < k) return 0; if (ncr[n][k] != -1) return ncr[n][k]; ncr[n][k] = f(n - 1, k) + f(n - 1, k - 1); // Since nCj = (n-1)Cj + (n-1)C(j-1); return ncr[n][k]; } // Function that summation of absolute // differences of all pairs raised // to the power k static long pairsPower() { long[][] pre = new long[n + 1][]; long ans = 0; for(int i = 0 ; i <= n ; i++){ pre[i] = new long[k + 1]; } // Sort the given array A.Sort(1, A.Count - 1, Comparer<long>.Default); // Precomputation part, O(n*k) for (int i = 1 ; i <= n ; ++i) { pre[i][0] = (long)1; for (int j = 1 ; j <= k ; j++) { pre[i][j] = A[i] * pre[i][j - 1]; } if (i != 1) { for (int j = 0 ; j <= k ; ++j){ pre[i][j] = pre[i][j] + pre[i - 1][j]; } } } // Traverse the array arr[] for (int i = n ; i >= 2 ; --i) { // For each K for (int j = 0 ; j <= k ; j++) { long val = f(k, j); long val1 = (long)(Math.Pow(A[i], k - j)) * pre[i - 1][j]; val = val * val1; if (j % 2 == 0) ans = (ans + val); else ans = (ans - val); } } ans = (long)(2) * ans; // Return the final answer return ans; } // Driver code public static void Main(string[] args){ // Given N and K n = 3; k = 3; // Initializing with -1 for(int i = 0 ; i <= 100 ; i++){ ncr[i] = new long[101]; for(int j = 0 ; j <= 100 ; j++){ ncr[i][j] = -1; } } // Given array // Making vector A as 1-Indexing // So adding 0 in the beginning A = new List<long>{ 0, 1, 2, 3 }; // Function Call Console.WriteLine(pairsPower()); } }
20
Complejidad temporal: O(N*K)
Espacio auxiliar: O(N*K)
Publicación traducida automáticamente
Artículo escrito por ssatyanand7 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA