Dada una array de enteros arr[] de longitud N y un entero K , la tarea es seleccionar algunas subarreglas que no se superpongan de modo que cada subarreglo tenga exactamente una longitud K , no haya dos subarreglos adyacentes y la suma de todos los elementos de los subconjuntos seleccionados es máximo.
Ejemplos:
Entrada: array[] = {1, 2, 3, 4, 5}, K = 2
Salida: 12
Sub-arrays que maximizan la suma serán {{1, 2}, {4, 5}}.
Por lo tanto, la respuesta será 12.
Entrada: arr[] = {1, 1, 1, 1, 1}, K = 1
Salida: 3
Enfoque: Este problema se puede resolver mediante programación dinámica . Supongamos que estamos en un índice i . Deje que dp[i] se defina como la suma máxima de elementos de todos los subconjuntos posibles de subarreglo arr[i…n-1] que satisfacen las condiciones anteriores.
Tendremos dos opciones posibles, es decir, seleccionar el subarreglo arr[i…i+k-1] y resolver para dp[i + k + 1] o rechazarlo y resolver para dp[i + 1] .
Por lo tanto, la relación de recurrencia será
dp[i] = max(dp[i + 1], arr[i] + arr[i + 1] + arr[i + 2] + … + arr[i + k – 1] + dp[i + k + 1])
Dado que los valores de K pueden ser grandes, usaremos la array de suma de prefijos para encontrar la suma de todos los elementos de la array secundaria [i…i + k – 1] en O(1).
En general, la complejidad temporal del algoritmo será O(N) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> #define maxLen 10 using namespace std; // To store the states of dp int dp[maxLen]; // To check if a given state // has been solved bool v[maxLen]; // To store the prefix-sum int prefix_sum[maxLen]; // Function to fill the prefix_sum[] with // the prefix sum of the given array void findPrefixSum(int arr[], int n) { prefix_sum[0] = arr[0]; for (int i = 1; i < n; i++) prefix_sum[i] = arr[i] + prefix_sum[i - 1]; } // Function to find the maximum sum subsequence // such that no two elements are adjacent int maxSum(int arr[], int i, int n, int k) { // Base case if (i + k > n) return 0; // To check if a state has // been solved if (v[i]) return dp[i]; v[i] = 1; int x; if (i == 0) x = prefix_sum[k - 1]; else x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; // Required recurrence relation dp[i] = max(maxSum(arr, i + 1, n, k), x + maxSum(arr, i + k + 1, n, k)); // Returning the value return dp[i]; } // Driver code int main() { int arr[] = { 1, 3, 7, 6 }; int n = sizeof(arr) / sizeof(int); int k = 1; // Finding prefix-sum findPrefixSum(arr, n); // Finding the maximum possible sum cout << maxSum(arr, 0, n, k); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { static int maxLen = 10; // To store the states of dp static int[] dp = new int[maxLen]; // To check if a given state // has been solved static boolean[] v = new boolean[maxLen]; // To store the prefix-sum static int[] prefix_sum = new int[maxLen]; // Function to fill the prefix_sum[] with // the prefix sum of the given array static void findPrefixSum(int arr[], int n) { prefix_sum[0] = arr[0]; for (int i = 1; i < n; i++) { prefix_sum[i] = arr[i] + prefix_sum[i - 1]; } } // Function to find the maximum sum subsequence // such that no two elements are adjacent static int maxSum(int arr[], int i, int n, int k) { // Base case if (i + k > n) { return 0; } // To check if a state has // been solved if (v[i]) { return dp[i]; } v[i] = true; int x; if (i == 0) { x = prefix_sum[k - 1]; } else { x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; } // Required recurrence relation dp[i] = Math.max(maxSum(arr, i + 1, n, k), x + maxSum(arr, i + k + 1, n, k)); // Returning the value return dp[i]; } // Driver code public static void main(String[] args) { int arr[] = {1, 3, 7, 6}; int n = arr.length; int k = 1; // Finding prefix-sum findPrefixSum(arr, n); // Finding the maximum possible sum System.out.println(maxSum(arr, 0, n, k)); } } // This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach maxLen = 10 # To store the states of dp dp = [0]*maxLen; # To check if a given state # has been solved v = [0]*maxLen; # To store the prefix-sum prefix_sum = [0]*maxLen; # Function to fill the prefix_sum[] with # the prefix sum of the given array def findPrefixSum(arr, n) : prefix_sum[0] = arr[0]; for i in range(n) : prefix_sum[i] = arr[i] + prefix_sum[i - 1]; # Function to find the maximum sum subsequence # such that no two elements are adjacent def maxSum(arr, i, n, k) : # Base case if (i + k > n) : return 0; # To check if a state has # been solved if (v[i]) : return dp[i]; v[i] = 1; if (i == 0) : x = prefix_sum[k - 1]; else : x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; # Required recurrence relation dp[i] = max(maxSum(arr, i + 1, n, k), x + maxSum(arr, i + k + 1, n, k)); # Returning the value return dp[i]; # Driver code if __name__ == "__main__" : arr = [ 1, 3, 7, 6 ]; n = len(arr); k = 1; # Finding prefix-sum findPrefixSum(arr, n); # Finding the maximum possible sum print(maxSum(arr, 0, n, k)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { static int maxLen = 10; // To store the states of dp static int[] dp = new int[maxLen]; // To check if a given state // has been solved static bool[] v = new bool[maxLen]; // To store the prefix-sum static int[] prefix_sum = new int[maxLen]; // Function to fill the prefix_sum[] with // the prefix sum of the given array static void findPrefixSum(int []arr, int n) { prefix_sum[0] = arr[0]; for (int i = 1; i < n; i++) { prefix_sum[i] = arr[i] + prefix_sum[i - 1]; } } // Function to find the maximum sum subsequence // such that no two elements are adjacent static int maxSum(int []arr, int i, int n, int k) { // Base case if (i + k > n) { return 0; } // To check if a state has // been solved if (v[i]) { return dp[i]; } v[i] = true; int x; if (i == 0) { x = prefix_sum[k - 1]; } else { x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; } // Required recurrence relation dp[i] = Math.Max(maxSum(arr, i + 1, n, k), x + maxSum(arr, i + k + 1, n, k)); // Returning the value return dp[i]; } // Driver code public static void Main(String[] args) { int []arr = {1, 3, 7, 6}; int n = arr.Length; int k = 1; // Finding prefix-sum findPrefixSum(arr, n); // Finding the maximum possible sum Console.Write(maxSum(arr, 0, n, k)); } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript implementation of the approach var maxLen = 10 // To store the states of dp var dp = Array(maxLen); // To check if a given state // has been solved var v = Array(maxLen); // To store the prefix-sum var prefix_sum = Array(maxLen);; // Function to fill the prefix_sum[] with // the prefix sum of the given array function findPrefixSum(arr, n) { prefix_sum[0] = arr[0]; for (var i = 1; i < n; i++) prefix_sum[i] = arr[i] + prefix_sum[i - 1]; } // Function to find the maximum sum subsequence // such that no two elements are adjacent function maxSum(arr, i, n, k) { // Base case if (i + k > n) return 0; // To check if a state has // been solved if (v[i]) return dp[i]; v[i] = 1; var x; if (i == 0) x = prefix_sum[k - 1]; else x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; // Required recurrence relation dp[i] = Math.max(maxSum(arr, i + 1, n, k), x + maxSum(arr, i + k + 1, n, k)); // Returning the value return dp[i]; } // Driver code var arr = [1, 3, 7, 6]; var n = arr.length; var k = 1; // Finding prefix-sum findPrefixSum(arr, n); // Finding the maximum possible sum document.write( maxSum(arr, 0, n, k)); </script>
9
Publicación traducida automáticamente
Artículo escrito por DivyanshuShekhar1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA