Dada una array arr[] de N enteros donde N es par, la tarea es agrupar los elementos de la array en los pares (X1, Y1), (X2, Y2), (X3, Y3), … tal que la suma min( X1, Y1) + min(X2, Y2) + min(X3, Y3) + … es máximo.
Ejemplos:
Entrada: arr[] = {1, 5, 3, 2}
Salida: 4
(1, 5) y (3, 2) -> 1 + 2 = 3
(1, 3) y (5, 2) -> 1 + 2 = 3
(1, 2) y (5, 3) -> 1 + 3 = 4
Entrada: arr[] = {1, 3, 2, 1, 4, 5}
Salida: 7
Enfoque: no importa cómo se formen los pares, el elemento máximo de la array siempre se ignorará, ya que será el elemento máximo en cada par en el que se coloque. Lo mismo ocurre con el segundo elemento máximo a menos que esté emparejado con el elemento máximo. Entonces, para maximizar la suma, un enfoque óptimo será ordenar la array y comenzar a hacer pares en orden a partir del elemento máximo.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum // required sum of the pairs int maxSum(int a[], int n) { // Sort the array sort(a, a + n); // To store the sum int sum = 0; // Start making pairs of every two // consecutive elements as n is even for (int i = 0; i < n - 1; i += 2) { // Minimum element of the current pair sum += a[i]; } // Return the maximum possible sum return sum; } // Driver code int main() { int arr[] = { 1, 3, 2, 1, 4, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout << maxSum(arr, n); return 0; }
Java
// Java implementation of the approach import java.util.Arrays; class GFG { // Function to return the maximum // required sum of the pairs static int maxSum(int a[], int n) { // Sort the array Arrays.sort(a); // To store the sum int sum = 0; // Start making pairs of every two // consecutive elements as n is even for (int i = 0; i < n - 1; i += 2) { // Minimum element of the current pair sum += a[i]; } // Return the maximum possible sum return sum; } // Driver code public static void main(String[] args) { int arr[] = { 1, 3, 2, 1, 4, 5 }; int n = arr.length; System.out.println(maxSum(arr, n)); } } // This code is contributed by Code_Mech
Python3
# Python3 implementation of the approach # Function to return the maximum # required sum of the pairs def maxSum(a, n) : # Sort the array a.sort(); # To store the sum sum = 0; # Start making pairs of every two # consecutive elements as n is even for i in range(0, n - 1, 2) : # Minimum element of the current pair sum += a[i]; # Return the maximum possible sum return sum; # Driver code if __name__ == "__main__" : arr = [ 1, 3, 2, 1, 4, 5 ]; n = len(arr); print(maxSum(arr, n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum // required sum of the pairs static int maxSum(int []a, int n) { // Sort the array Array.Sort(a); // To store the sum int sum = 0; // Start making pairs of every two // consecutive elements as n is even for (int i = 0; i < n - 1; i += 2) { // Minimum element of the current pair sum += a[i]; } // Return the maximum possible sum return sum; } // Driver code public static void Main(String[] args) { int []arr = { 1, 3, 2, 1, 4, 5 }; int n = arr.Length; Console.WriteLine(maxSum(arr, n)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function to return the maximum // required sum of the pairs function maxSum(a, n) { // Sort the array a.sort((a, b) => a - b); // To store the sum let sum = 0; // Start making pairs of every two // consecutive elements as n is even for (let i = 0; i < n - 1; i += 2) { // Minimum element of the current pair sum += a[i]; } // Return the maximum possible sum return sum; } // Driver code let arr = [1, 3, 2, 1, 4, 5]; let n = arr.length; document.write(maxSum(arr, n)); // This code is contributed by _saurabh_jaiswal </script>
7
Complejidad de Tiempo: O(n logn)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por PLACEMENT__CHAHIYE y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA