Dados cinco enteros X , Y , A , B y N , la tarea es encontrar la máxima diferencia absoluta posible entre X e Y realizando las siguientes operaciones exactamente N veces:
- Decrementa el valor de X en 1 hasta A .
- Disminuya el valor de Y en 1 hasta B .
Nota: El valor de (X – A + Y – B) debe ser mayor o igual a N
Ejemplos:
Entrada: X = 12, Y = 8, A = 8, B = 7, N = 2
Salida: 4
Explicación:
Decrementando el valor de X en 1. Por lo tanto, X = X – 1 = 11
Decrementando el valor de Y en 1 Por lo tanto, Y = Y – 1 = 7
Por lo tanto, la máxima diferencia absoluta entre X e Y = abs(X – Y) = abs(11 – 7) = 4Entrada: X = 10, Y = 10, A = 8, B = 5, N = 3
Salida: 3
Explicación:
Decrementar el valor de Y en 1 tres veces. Por lo tanto, Y = Y – 3 = 7
Por lo tanto, la máxima diferencia absoluta entre X e Y = abs(X – Y) = abs(10 – 7) = 3
Planteamiento: El problema se puede resolver usando la técnica Greedy . Siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos n1 para almacenar el recuento máximo de operaciones realizadas en X .
- Actualizar n1 = min(N, X – A) .
- Inicialice una variable, digamos n2 para almacenar el recuento máximo de operaciones realizadas en Y .
- Actualizar n2 = min(N, Y – B) .
- Inicialice una variable, digamos, diff_X_Y_1 para almacenar la diferencia absoluta de X e Y al disminuir primero el valor de X en 1 exactamente min (N, n1) veces y luego disminuir el valor de Y por los tiempos restantes de las operaciones.
- Inicialice una variable, digamos, diff_X_Y_2 para almacenar la diferencia absoluta de X e Y al disminuir primero el valor de Y en 1 exactamente min (N, n2) veces y luego disminuir el valor de X por los tiempos restantes de las operaciones.
- Finalmente, imprime el valor de max(diff_X_Y_1, diff_X_Y_2) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the absolute difference // between X and Y with given operations int AbsDiff(int X, int Y, int A, int B, int N) { // Stores maximum absolute difference // between X and Y with given operations int maxDiff = 0; // Stores maximum number of operations // performed on X int n1 = X - A; // Update X X = X - min(N, n1); // Decrementing N at most N times N = N - min(N, n1); // Stores maximum number of operations // performed on Y int n2 = Y - B; // Update Y Y = Y - min(N, n2); // Update maxDiff maxDiff = abs(X - Y); return maxDiff; } // Function to find the max absolute difference // between X and Y with given operations int maxAbsDiff(int X, int Y, int A, int B, int N) { // Stores absolute difference between // X and Y by first decrementing X and then Y int diffX_Y_1; // Stores absolute difference between X // and Y first decrementing Y and then X int diffX_Y_2; // Update diffX_Y_1 diffX_Y_1 = AbsDiff(X, Y, A, B, N); // Swap X, Y and A, B swap(X, Y); swap(A, B); // Update diffX_Y_2 diffX_Y_2 = AbsDiff(X, Y, A, B, N); return max(diffX_Y_1, diffX_Y_2); } // Driver Code int main() { int X = 10; int Y = 10; int A = 8; int B = 5; int N = 3; cout << maxAbsDiff(X, Y, A, B, N); }
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to find the absolute difference // between X and Y with given operations public static int AbsDiff(int X, int Y, int A, int B, int N) { // Stores maximum absolute difference // between X and Y with given operations int maxDiff = 0; // Stores maximum number of operations // performed on X int n1 = X - A; // Update X X = X - Math.min(N, n1); // Decrementing N at most N times N = N - Math.min(N, n1); // Stores maximum number of operations // performed on Y int n2 = Y - B; // Update Y Y = Y - Math.min(N, n2); // Update maxDiff maxDiff = Math.abs(X - Y); return maxDiff; } // Function to find the max absolute difference // between X and Y with given operations public static int maxAbsDiff(int X, int Y, int A, int B, int N) { // Stores absolute difference between // X and Y by first decrementing X and then Y int diffX_Y_1; // Stores absolute difference between X // and Y first decrementing Y and then X int diffX_Y_2; // Update diffX_Y_1 diffX_Y_1 = AbsDiff(X, Y, A, B, N); // Swap X, Y and A, B int temp1 = X; X = Y; Y = temp1; int temp2 = A; A = B; B = temp2; // Update diffX_Y_2 diffX_Y_2 = AbsDiff(X, Y, A, B, N); return Math.max(diffX_Y_1, diffX_Y_2); } // Driver code public static void main(String[] args) { int X = 10; int Y = 10; int A = 8; int B = 5; int N = 3; System.out.println(maxAbsDiff(X, Y, A, B, N)); } } // This code is contributed by divyeshrabadiya07
Python3
# Python3 program to implement # the above approach # Function to find the absolute difference # between X and Y with given operations def AbsDiff(X, Y, A, B, N): # Stores maximum absolute difference # between X and Y with given operations maxDiff = 0 # Stores maximum number of operations # performed on X n1 = X - A # Update X X = X - min(N, n1) # Decrementing N at most N times N = N - min(N, n1) # Stores maximum number of operations # performed on Y n2 = Y - B # Update Y Y = Y - min(N, n2) # Update maxDiff maxDiff = abs(X - Y) return maxDiff # Function to find the max absolute difference # between X and Y with given operations def maxAbsDiff(X, Y, A, B, N): # Stores absolute difference between # X and Y by first decrementing X and then Y diffX_Y_1 = AbsDiff(X, Y, A, B, N) # Swap X, Y and A, B temp1 = X X = Y Y = temp1 temp2 = A A = B B = temp2 # Stores absolute difference between X # and Y first decrementing Y and then X diffX_Y_2 = AbsDiff(X, Y, A, B, N) return max(diffX_Y_1, diffX_Y_2) # Driver Code X = 10 Y = 10 A = 8 B = 5 N = 3 print(maxAbsDiff(X, Y, A, B, N)) # This code is contributed by sanjoy_62
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find the absolute difference // between X and Y with given operations public static int AbsDiff(int X, int Y, int A, int B, int N) { // Stores maximum absolute difference // between X and Y with given operations int maxDiff = 0; // Stores maximum number of operations // performed on X int n1 = X - A; // Update X X = X - Math.Min(N, n1); // Decrementing N at most N times N = N - Math.Min(N, n1); // Stores maximum number of operations // performed on Y int n2 = Y - B; // Update Y Y = Y - Math.Min(N, n2); // Update maxDiff maxDiff = Math.Abs(X - Y); return maxDiff; } // Function to find the max absolute difference // between X and Y with given operations public static int maxAbsDiff(int X, int Y, int A, int B, int N) { // Stores absolute difference between // X and Y by first decrementing X and then Y int diffX_Y_1; // Stores absolute difference between X // and Y first decrementing Y and then X int diffX_Y_2; // Update diffX_Y_1 diffX_Y_1 = AbsDiff(X, Y, A, B, N); // Swap X, Y and A, B int temp1 = X; X = Y; Y = temp1; int temp2 = A; A = B; B = temp2; // Update diffX_Y_2 diffX_Y_2 = AbsDiff(X, Y, A, B, N); return Math.Max(diffX_Y_1, diffX_Y_2); } // Driver code public static void Main(String[] args) { int X = 10; int Y = 10; int A = 8; int B = 5; int N = 3; Console.WriteLine(maxAbsDiff(X, Y, A, B, N)); } } // This code is contributed by Amit Katiyar
Javascript
<script> // Javascript program to implement // the above approach // Function to find the absolute difference // between X and Y with given operations function AbsDiff(X, Y, A, B, N) { // Stores maximum absolute difference // between X and Y with given operations let maxDiff = 0; // Stores maximum number of operations // performed on X let n1 = X - A; // Update X X = X - Math.min(N, n1); // Decrementing N at most N times N = N - Math.min(N, n1); // Stores maximum number of operations // performed on Y let n2 = Y - B; // Update Y Y = Y - Math.min(N, n2); // Update maxDiff maxDiff = Math.abs(X - Y); return maxDiff; } // Function to find the max absolute difference // between X and Y with given operations function maxAbsDiff(X, Y, A, B, N) { // Stores absolute difference between // X and Y by first decrementing X and then Y let diffX_Y_1; // Stores absolute difference between X // and Y first decrementing Y and then X let diffX_Y_2; // Update diffX_Y_1 diffX_Y_1 = AbsDiff(X, Y, A, B, N); // Swap X, Y and A, B let temp1 = X; X = Y; Y = temp1; let temp2 = A; A = B; B = temp2; // Update diffX_Y_2 diffX_Y_2 = AbsDiff(X, Y, A, B, N); return Math.max(diffX_Y_1, diffX_Y_2); } // Driver Code let X = 10; let Y = 10; let A = 8; let B = 5; let N = 3; document.write(maxAbsDiff(X, Y, A, B, N)); </script>
3
Complejidad temporal: O(1)
Espacio auxiliar: O(1)