Dados dos enteros N y K , la tarea es maximizar la suma de las diferencias absolutas entre los elementos adyacentes de una array de longitud N y suma K .
Ejemplos:
Entrada: N = 5, K = 10
Salida: 20
Explicación:
La array arr[] con suma 10 puede ser {0, 5, 0, 5, 0}, maximizando la suma de la diferencia absoluta de elementos adyacentes ( 5 + 5 + 5 + 5 = 20)
Entrada: N = 2, K = 10
Salida: 10
Enfoque:
Para maximizar la suma de elementos adyacentes, siga los pasos a continuación:
- Si N es 2, la máxima suma posible es K colocando K en 1 índice y 0 en el otro.
- Si N es 1, la suma máxima posible siempre será 0.
- Para todos los demás valores de N , la respuesta será 2 * K.
Ilustración:
Para N = 3 , el arreglo {0, K, 0} maximiza la suma de la diferencia absoluta entre elementos adyacentes a 2 * K .
Para N = 4 , el arreglo {0, K/2, 0, K/2} o {0, K, 0, 0} maximiza la suma requerida de diferencia absoluta entre elementos adyacentes a 2 * K.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to maximize the // sum of absolute differences // between adjacent elements #include <bits/stdc++.h> using namespace std; // Function for maximizing the sum int maxAdjacentDifference(int N, int K) { // Difference is 0 when only // one element is present // in array if (N == 1) { return 0; } // Difference is K when // two elements are // present in array if (N == 2) { return K; } // Otherwise return 2 * K; } // Driver code int main() { int N = 6; int K = 11; cout << maxAdjacentDifference(N, K); return 0; }
Java
// Java program to maximize the // sum of absolute differences // between adjacent elements import java.util.*; class GFG{ // Function for maximising the sum static int maxAdjacentDifference(int N, int K) { // Difference is 0 when only // one element is present // in array if (N == 1) { return 0; } // Difference is K when // two elements are // present in array if (N == 2) { return K; } // Otherwise return 2 * K; } // Driver code public static void main(String[] args) { int N = 6; int K = 11; System.out.print(maxAdjacentDifference(N, K)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program to maximize the # sum of absolute differences # between adjacent elements # Function for maximising the sum def maxAdjacentDifference(N, K): # Difference is 0 when only # one element is present # in array if (N == 1): return 0; # Difference is K when # two elements are # present in array if (N == 2): return K; # Otherwise return 2 * K; # Driver code N = 6; K = 11; print(maxAdjacentDifference(N, K)); # This code is contributed by Code_Mech
C#
// C# program to maximize the // sum of absolute differences // between adjacent elements using System; class GFG{ // Function for maximising the sum static int maxAdjacentDifference(int N, int K) { // Difference is 0 when only // one element is present // in array if (N == 1) { return 0; } // Difference is K when // two elements are // present in array if (N == 2) { return K; } // Otherwise return 2 * K; } // Driver code public static void Main(String[] args) { int N = 6; int K = 11; Console.Write(maxAdjacentDifference(N, K)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // JavaScript program to maximize the // sum of absolute differences // between adjacent elements // Function for maximising the sum function maxAdjacentDifference(N, K) { // Difference is 0 when only // one element is present // in array if (N == 1) { return 0; } // Difference is K when // two elements are // present in array if (N == 2) { return K; } // Otherwise return 2 * K; } // Driver Code let N = 6; let K = 11; document.write(maxAdjacentDifference(N, K)); // This code is contributed by susmitakundugoaldanga. </script>
22
Tiempo Complejidad: O(1) .
Publicación traducida automáticamente
Artículo escrito por mayur_patil y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA