Given five integers X, Y, A, B, and N, the task is to find the maximum possible absolute difference between X and Y by performing the following the operations exactly N times:
- Decrement the value of X by 1 up to A.
- Decrement the value of Y by 1 up to B.
Note: The value of (X – A + Y – B) must be greater than or equal to N
Input: X = 12, Y = 8, A = 8, B = 7, N = 2
Decrementing the value of X by 1. Therefore, X = X – 1 = 11
Decrementing the value of Y by 1. Therefore, Y = Y – 1 = 7
Therefore, the maximum absolute difference between X and Y = abs(X – Y) = abs(11 – 7) = 4
Input: X = 10, Y = 10, A = 8, B = 5, N = 3
Decrementing the value of Y by 1 three times. Therefore, Y = Y – 3 = 7
Therefore, the maximum absolute difference between X and Y = abs(X – Y) = abs(10 – 7) = 3
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Initialize a variable, say n1 to store the maximum count of operations performed on X.
- Update n1 = min(N, X – A).
- Initialize a variable, say n2 to store the maximum count of operations performed on Y.
- Update n2 = min(N, Y – B).
- Initialize a variable say, diff_X_Y_1 to store the absolute difference of X and Y by first decrementing the value of X by 1 exactly min(N, n1) times then decrement the value of Y by the remaining times of operations.
- Initialize a variable say, diff_X_Y_2 to store the absolute difference of X and Y by first decrementing the value of Y by 1 exactly min(N, n2) times then decrement the value of X by the remaining times of operations.
- Finally, print the value of max(diff_X_Y_1, diff_X_Y_2).
Below is the implementation of the above approach :
Time complexity: O(1)
- Minimize positive product of two given numbers by at most N decrements
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Minimize increments or decrements required to make sum and product of array elements non-zero
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Maximize product of absolute index difference with K
- Minimum decrements to make integer A divisible by integer B
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Count decrements to nearest smaller element required to make all array elements equal
- Minimize increments or decrements by 2 to convert given value to a perfect square
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Maximize number of elements from Array with sum at most K
- Maximize sum of topmost elements of S stacks by popping at most N elements
- Maximize Array sum by swapping at most K elements with another array
- Maximize length of subarray of equal elements by performing at most K increment operations
- Maximize length of subarray having equal elements by adding at most K
- Maximize sum of assigned weights by flipping at most K bits in given Binary String
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.