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 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
Examples:
Input: X = 12, Y = 8, A = 8, B = 7, N = 2
Output: 4
Explanation:
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
Output: 3
Explanation:
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 the 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 :
C++
#include <bits/stdc++.h>
using namespace std;
int AbsDiff( int X, int Y, int A, int B, int N)
{
int maxDiff = 0;
int n1 = X - A;
X = X - min(N, n1);
N = N - min(N, n1);
int n2 = Y - B;
Y = Y - min(N, n2);
maxDiff = abs (X - Y);
return maxDiff;
}
int maxAbsDiff( int X, int Y, int A, int B, int N)
{
int diffX_Y_1;
int diffX_Y_2;
diffX_Y_1 = AbsDiff(X, Y, A, B, N);
swap(X, Y);
swap(A, B);
diffX_Y_2 = AbsDiff(X, Y, A, B, N);
return max(diffX_Y_1, diffX_Y_2);
}
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
import java.util.*;
class GFG{
public static int AbsDiff( int X, int Y, int A,
int B, int N)
{
int maxDiff = 0 ;
int n1 = X - A;
X = X - Math.min(N, n1);
N = N - Math.min(N, n1);
int n2 = Y - B;
Y = Y - Math.min(N, n2);
maxDiff = Math.abs(X - Y);
return maxDiff;
}
public static int maxAbsDiff( int X, int Y, int A,
int B, int N)
{
int diffX_Y_1;
int diffX_Y_2;
diffX_Y_1 = AbsDiff(X, Y, A, B, N);
int temp1 = X;
X = Y;
Y = temp1;
int temp2 = A;
A = B;
B = temp2;
diffX_Y_2 = AbsDiff(X, Y, A, B, N);
return Math.max(diffX_Y_1, diffX_Y_2);
}
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));
}
}
|
Python3
def AbsDiff(X, Y, A, B, N):
maxDiff = 0
n1 = X - A
X = X - min (N, n1)
N = N - min (N, n1)
n2 = Y - B
Y = Y - min (N, n2)
maxDiff = abs (X - Y)
return maxDiff
def maxAbsDiff(X, Y, A, B, N):
diffX_Y_1 = AbsDiff(X, Y, A, B, N)
temp1 = X
X = Y
Y = temp1
temp2 = A
A = B
B = temp2
diffX_Y_2 = AbsDiff(X, Y, A, B, N)
return max (diffX_Y_1, diffX_Y_2)
X = 10
Y = 10
A = 8
B = 5
N = 3
print (maxAbsDiff(X, Y, A, B, N))
|
C#
using System;
class GFG{
public static int AbsDiff( int X, int Y, int A,
int B, int N)
{
int maxDiff = 0;
int n1 = X - A;
X = X - Math.Min(N, n1);
N = N - Math.Min(N, n1);
int n2 = Y - B;
Y = Y - Math.Min(N, n2);
maxDiff = Math.Abs(X - Y);
return maxDiff;
}
public static int maxAbsDiff( int X, int Y, int A,
int B, int N)
{
int diffX_Y_1;
int diffX_Y_2;
diffX_Y_1 = AbsDiff(X, Y, A, B, N);
int temp1 = X;
X = Y;
Y = temp1;
int temp2 = A;
A = B;
B = temp2;
diffX_Y_2 = AbsDiff(X, Y, A, B, N);
return Math.Max(diffX_Y_1, diffX_Y_2);
}
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));
}
}
|
Javascript
<script>
function AbsDiff(X, Y, A, B, N)
{
let maxDiff = 0;
let n1 = X - A;
X = X - Math.min(N, n1);
N = N - Math.min(N, n1);
let n2 = Y - B;
Y = Y - Math.min(N, n2);
maxDiff = Math.abs(X - Y);
return maxDiff;
}
function maxAbsDiff(X, Y, A, B, N)
{
let diffX_Y_1;
let diffX_Y_2;
diffX_Y_1 = AbsDiff(X, Y, A, B, N);
let temp1 = X;
X = Y;
Y = temp1;
let temp2 = A;
A = B;
B = temp2;
diffX_Y_2 = AbsDiff(X, Y, A, B, N);
return Math.max(diffX_Y_1, diffX_Y_2);
}
let X = 10;
let Y = 10;
let A = 8;
let B = 5;
let N = 3;
document.write(maxAbsDiff(X, Y, A, B, N));
</script>
|
Time complexity: O(1)
Auxiliary Space:O(1)