Dada una array arr[] de tamaño N, la tarea es encontrar la suma máxima de una subsecuencia formada por la concatenación de subarreglos disjuntos cuyas longitudes son números primos .
Ejemplos:
Entrada: arr[] = {10, 10, 7, 10, 10, 10}
Salida: 50
Explicación:
La subsecuencia con suma máxima se obtiene concatenando los siguientes dos subarreglos:
- {10, 10} de longitud 2, que es primo.
- {10, 10, 10} de longitud 3, que es primo.
La subsecuencia resultante es {10, 10, 10, 10, 10}.
La suma de la subsecuencia es 50.Entrada: arr[] = {11, 8, 12}
Salida: 31
Enfoque ingenuo: el enfoque más simple es usar la recursividad para calcular el valor de la suma máxima de la subsecuencia. En cada paso, realice varias llamadas recursivas para cada uno de los números primos menores que N . La relación de recurrencia está dada por:
sum(N) = (arr[N] + arr[N – 1] + … arr[N – P + 1]) + sum(N – P – 1)
donde,
P es la longitud prima del subarreglo elegido,
sum (N) es la función que encuentra la suma máxima de la subsecuencia resultante.
La relación de recurrencia anterior es para un solo número primo . Por lo tanto, puede haber más de una posible subsecuencia que se puede formar seleccionando diferentes subarreglos de diferente longitud principal. A continuación se muestra la relación de recurrencia resultante formada:
sum(N) = max(sum(a N , …, a N-P1+1 ) + sum(N – P1 – 1), sum(a N , …, a N-P2+1 ) + sum(N – P2 – 1), …, sum(a N , …, a N-Pk+1 ) + sum(N – P k – 1))
donde,
P1, P2, … Pk son números primos menores que N.
Complejidad Temporal: O(K N ) donde K es el número de losnúmeros primos menores que N. Aproximadamente K = (N / LogN)
Espacio Auxiliar: O (1)
Programación dinámica usando el enfoque de abajo hacia arriba: las llamadas recursivas de arriba también se pueden reducir usando una array auxiliar dp[] y calcular el valor de cada estado en el enfoque de abajo hacia arriba . A continuación se muestran los pasos:
- Cree una array auxiliar prime[] para almacenar todos los números primos menores o iguales a N.
- Mantenga el Tamiz de Eratóstenes y atraviéselo para completar los valores de array prime[] .
- Cree una array auxiliar dp[] de tamaño N .
- Inicialice el estado 0 y 1 como dp[0] = 0 y dp[1] = 0 .
- Recorra la array dp[] sobre el rango [2, N] y actualice cada estado como:
MSS(i) = max[sum(a i …a i-P1+1 ) + sum(i-P1-1), sum(a i …a i-P2+1 ) + sum(i-P2-1) , … sum(a i …a i-Pk+1 ) + sum(i-Pk-1) ] para todos los números primos P1, P2, … Pk menores que N.
- Inicialice la array pref[] para almacenar la suma del prefijo para calcular la suma (l, …, r) de manera eficiente.
a i + a i+1 + … a j = sum(i … j) = pref[j] – pref[i – 1]
- Imprima el valor de dp[N] después de los pasos anteriores como resultado.
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; #define MAX 100005 // Function to return all prime numbers // smaller than N vector<int> SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" bool sieve[MAX]; // Initialize all its entries as true memset(sieve, true, sizeof(sieve)); for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (sieve[p] == true) { // Update all multiples of // p greater than or equal // to the square of it for (int i = p * p; i < MAX; i += p) { sieve[i] = false; } } } // Stores all prime numbers // smaller than MAX vector<int> v; // Store all prime numbers for (int p = 2; p < MAX; p++) { // If p is prime if (sieve[p]) { v.push_back(p); } } return v; } // Function to build the auxiliary DP // array from the start void build(int dp[], int arr[], int N) { // Base Case dp[0] = 0; dp[1] = 0; // Stores all prime numbers < N vector<int> prime = SieveOfEratosthenes(); // Stores prefix sum int pref[N + 1]; pref[0] = 0; // Update prefix sum for (int i = 1; i <= N; i++) { pref[i] = pref[i - 1] + arr[i - 1]; } // Iterate over range for (int i = 2; i <= N; i++) { // Update each state i.e.. when // current element is excluded dp[i] = dp[i - 1]; for (int j = 0; j <= prime.size(); j++) { // Find start & end index // of subarrays when prime[i] // is taken int r = i - 1; int l = r - prime[j] + 1; // Check if starting point // lies in the array if (l < 0) break; int temp = 0; // Include the elements // al al+1 ... ar temp = pref[r + 1] - pref[l]; // Check if element lies before // start of selected subarray if (l - 2 >= 0) temp += dp[l - 2 + 1]; // Update value of dp[i] dp[i] = max(dp[i], temp); } } } // Function to find the maximum sum // subsequence with prime length void maxSumSubseq(int arr[], int N) { // Auxiliary DP array int dp[N + 1]; // Build DP array build(dp, arr, N); // Print the result cout << dp[N]; } // Driver Code int main() { // Given arr[] int arr[] = { 10, 10, 7, 10, 10, 10 }; // Size of array int N = sizeof(arr) / sizeof(arr[0]); // Function Call maxSumSubseq(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ static int MAX = 100005; // Function to return all prime numbers // smaller than N static Vector<Integer> SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" boolean []sieve = new boolean[MAX]; // Initialize all its entries as true Arrays.fill(sieve, true); for(int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (sieve[p] == true) { // Update all multiples of // p greater than or equal // to the square of it for(int i = p * p; i < MAX; i += p) { sieve[i] = false; } } } // Stores all prime numbers // smaller than MAX Vector<Integer> v = new Vector<Integer>(); // Store all prime numbers for(int p = 2; p < MAX; p++) { // If p is prime if (sieve[p]) { v.add(p); } } return v; } // Function to build the auxiliary DP // array from the start static void build(int dp[], int arr[], int N) { // Base Case dp[0] = 0; dp[1] = 0; // Stores all prime numbers < N Vector<Integer> prime = SieveOfEratosthenes(); // Stores prefix sum int []pref = new int[N + 1]; pref[0] = 0; // Update prefix sum for(int i = 1; i <= N; i++) { pref[i] = pref[i - 1] + arr[i - 1]; } // Iterate over range for(int i = 2; i <= N; i++) { // Update each state i.e.. when // current element is excluded dp[i] = dp[i - 1]; for(int j = 0; j <= prime.size(); j++) { // Find start & end index // of subarrays when prime[i] // is taken int r = i - 1; int l = r - prime.get(j) + 1; // Check if starting point // lies in the array if (l < 0) break; int temp = 0; // Include the elements // al al+1 ... ar temp = pref[r + 1] - pref[l]; // Check if element lies before // start of selected subarray if (l - 2 >= 0) temp += dp[l - 2 + 1]; // Update value of dp[i] dp[i] = Math.max(dp[i], temp); } } } // Function to find the maximum sum // subsequence with prime length static void maxSumSubseq(int arr[], int N) { // Auxiliary DP array int []dp = new int[N + 1]; // Build DP array build(dp, arr, N); // Print the result System.out.print(dp[N]); } // Driver Code public static void main(String args[]) { // Given arr[] int arr[] = { 10, 10, 7, 10, 10, 10 }; // Size of array int N = arr.length; // Function Call maxSumSubseq(arr, N); } } // This code is contributed by ipg2016107
Python3
# Python3 program for the above approach MAX = 100005 # Function to return all prime numbers # smaller than N def SieveOfEratosthenes(): # Create a boolean array "prime[0..n]" sieve = [True for i in range(MAX)] # Initialize all its entries as true # memset(sieve, true, sizeof(sieve)) for p in range(2, MAX): if p * p > MAX: break # If prime[p] is not changed, # then it is a prime if (sieve[p] == True): # Update all multiples of # p greater than or equal # to the square of it for i in range(p * p, MAX, p): sieve[i] = False # Stores all prime numbers # smaller than MAX v = [] # Store all prime numbers for p in range(2, MAX): # If p is prime if (sieve[p]): v.append(p) return v # Function to build the auxiliary DP # array from the start def build(dp, arr, N): # Base Case dp[0] = 0 dp[1] = 0 # Stores all prime numbers < N prime = SieveOfEratosthenes() # Stores prefix sum pref = [0 for i in range(N + 1)] pref[0] = 0 # Update prefix sum for i in range(1, N + 1): pref[i] = pref[i - 1] + arr[i - 1] # Iterate over range for i in range(2, N + 1): # Update each state i.e.. when # current element is excluded dp[i] = dp[i - 1] for j in range(len(prime) + 1): # Find start & end index # of subarrays when prime[i] # is taken r = i - 1 l = r - prime[j] + 1 # Check if starting point # lies in the array if (l < 0): break temp = 0 # Include the elements # al al+1 ... ar temp = pref[r + 1] - pref[l] # Check if element lies before # start of selected subarray if (l - 2 >= 0): temp += dp[l - 2 + 1] # Update value of dp[i] dp[i] = max(dp[i], temp) # Function to find the maximum sum # subsequence with prime length def maxSumSubseq(arr, N): # Auxiliary DP array dp = [0 for i in range(N + 1)] # Build DP array build(dp, arr, N) # Print the result print(dp[N]) # Driver Code if __name__ == '__main__': # Given arr[] arr = [ 10, 10, 7, 10, 10, 10 ] # Size of array N = len(arr) # Function Call maxSumSubseq(arr, N) # This code is contributed by mohit kumar 29
C#
// C# program for the // above approach using System; using System.Collections.Generic; class GFG{ static int MAX = 100005; // Function to return all // prime numbers smaller than N static List<int> SieveOfEratosthenes() { // Create a bool array // "prime[0..n]" bool []sieve = new bool[MAX]; // Initialize all its entries // as true for(int i = 0; i < MAX; i++) sieve[i] = true; for(int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (sieve[p] == true) { // Update all multiples of // p greater than or equal // to the square of it for(int i = p * p; i < MAX; i += p) { sieve[i] = false; } } } // Stores all prime numbers // smaller than MAX List<int> v = new List<int>(); // Store all prime numbers for(int p = 2; p < MAX; p++) { // If p is prime if (sieve[p]) { v.Add(p); } } return v; } // Function to build the auxiliary // DP array from the start static void build(int []dp, int []arr, int N) { // Base Case dp[0] = 0; dp[1] = 0; // Stores all prime // numbers < N List<int> prime = SieveOfEratosthenes(); // Stores prefix sum int []pref = new int[N + 1]; pref[0] = 0; // Update prefix sum for(int i = 1; i <= N; i++) { pref[i] = pref[i - 1] + arr[i - 1]; } // Iterate over range for(int i = 2; i <= N; i++) { // Update each state i.e.. // when current element // is excluded dp[i] = dp[i - 1]; for(int j = 0; j <= prime.Count; j++) { // Find start & end index // of subarrays when prime[i] // is taken int r = i - 1; int l = r - prime[j] + 1; // Check if starting point // lies in the array if (l < 0) break; int temp = 0; // Include the elements // al al+1 ... ar temp = pref[r + 1] - pref[l]; // Check if element lies // before start of selected // subarray if (l - 2 >= 0) temp += dp[l - 2 + 1]; // Update value of dp[i] dp[i] = Math.Max(dp[i], temp); } } } // Function to find the maximum // sum subsequence with prime // length static void maxSumSubseq(int []arr, int N) { // Auxiliary DP array int []dp = new int[N + 1]; // Build DP array build(dp, arr, N); // Print the result Console.Write(dp[N]); } // Driver Code public static void Main(String []args) { // Given []arr int []arr = {10, 10, 7, 10, 10, 10}; // Size of array int N = arr.Length; // Function Call maxSumSubseq(arr, N); } } // This code is contributed by shikhasingrajput
Javascript
<script> // Javascript program for the above approach let MAX = 100005 // Function to return all prime numbers // smaller than N function SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" let sieve = new Array(MAX); // Initialize all its entries as true sieve.fill(true) for (let p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (sieve[p] == true) { // Update all multiples of // p greater than or equal // to the square of it for (let i = p * p; i < MAX; i += p) { sieve[i] = false; } } } // Stores all prime numbers // smaller than MAX let v = new Array(); // Store all prime numbers for (let p = 2; p < MAX; p++) { // If p is prime if (sieve[p]) { v.push(p); } } return v; } // Function to build the auxiliary DP // array from the start function build(dp, arr, N) { // Base Case dp[0] = 0; dp[1] = 0; // Stores all prime numbers < N let prime = SieveOfEratosthenes(); // Stores prefix sum let pref = new Array(N + 1); pref[0] = 0; // Update prefix sum for (let i = 1; i <= N; i++) { pref[i] = pref[i - 1] + arr[i - 1]; } // Iterate over range for (let i = 2; i <= N; i++) { // Update each state i.e.. when // current element is excluded dp[i] = dp[i - 1]; for (let j = 0; j <= prime.length; j++) { // Find start & end index // of subarrays when prime[i] // is taken let r = i - 1; let l = r - prime[j] + 1; // Check if starting point // lies in the array if (l < 0) break; let temp = 0; // Include the elements // al al+1 ... ar temp = pref[r + 1] - pref[l]; // Check if element lies before // start of selected subarray if (l - 2 >= 0) temp += dp[l - 2 + 1]; // Update value of dp[i] dp[i] = Math.max(dp[i], temp); } } } // Function to find the maximum sum // subsequence with prime length function maxSumSubseq(arr, N) { // Auxiliary DP array let dp = new Array(N + 1); // Build DP array build(dp, arr, N); // Print the result document.write(dp[N]); } // Driver Code // Given arr[] let arr = [ 10, 10, 7, 10, 10, 10 ]; // Size of array let N = arr.length; // Function Call maxSumSubseq(arr, N); // This code is contributed by gfgking </script>
50
Complejidad temporal: O(N * K)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por math_lover y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA