Min operations to convert A to B by incrementing A in the given range
Last Updated :
24 Apr, 2023
Given two integers A and B and a range defined by two integers C and D, the task is to convert A to B using the minimum number of operations possible. The only allowed operations are to add or subtract any integer greater than or equal to E to A. However, after each operation, the resulting value of A must lie in the range C to D (inclusive). If it is not possible to obtain B from A using these operations, return -1.
Examples:
Input: A = 4, B = 5, C = 0, D = 15, E = 5
Output: 2
Explanation: In first operation add 6 to A. A becomes 10. In second operation, subtract 5 from A. Now A becomes 5, which is equal to B. Note that after both operations, A lies in the range [C, D]. Also, the numbers added or subtracted (6 and 5) are greater than or equal to E.
Input: A = 3, B = 4, C = 3, D = 5, E = 6
Output: -1
Approach: To solve the problem follow the below idea:
- If A = B, in this case, 0 operations are required to convert A to B.
- If the absolute difference between A and B is greater than or equal to E, 1 operation is sufficient to make A equal to B. The integer value E = |B-A| can be added or subtracted to/from A to make it equal to B.
- There exists an integer X between C and D, such that |A-X| ≥ E and |B-X| ≥ E, in this case, A can be made B in 2 operations: A -> C -> B or A -> D -> B.
- If we cannot make A equal to B through one of the boundaries as in 3rd case, we will require 3 operations to do so, i.e. we will go through both the boundaries C and D:
A -> C -> D -> B or A -> D -> C -> B.
- If any of the above conditions are not satisfied, then it is not possible to convert A to B. Hence, we will return -1.
Below are the steps for the above approach:
- If A is equal to B, return 0.
- If the absolute difference between A and B is greater than or equal to E, return 1.
- If the difference between D and the greater of A and B is greater than or equal to E, or if the difference between the lesser of A and B and C is greater than or equal to E, return 2.
- Check if (D-B ≥ E && A-C ≥ E) or (D-A ≥ E && B-C ≥ E) is true, and return 3.
- Else returns -1.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int minOperations( int A, int B, int C, int D, int E)
{
if (A == B) {
return 0;
}
else if ( abs (A - B) >= E) {
return 1;
}
else if (D - max(A, B) >= E || min(A, B) - C >= E) {
return 2;
}
else if ((D - B >= E && A - C >= E)
|| (D - A >= E && B - C >= E)) {
return 3;
}
else {
return -1;
}
}
int32_t main()
{
int A = 4, B = 5, C = 0, D = 15, E = 5;
int answer = minOperations(A, B, C, D, E);
cout << answer << endl;
return 0;
}
|
Java
import java.util.*;
class Main {
public static long minOperations( long A, long B, long C,
long D, long E)
{
if (A == B) {
return 0 ;
}
else if (Math.abs(A - B) >= E) {
return 1 ;
}
else if (D - Math.max(A, B) >= E
|| Math.min(A, B) - C >= E) {
return 2 ;
}
else if ((D - B >= E && A - C >= E)
|| (D - A >= E && B - C >= E)) {
return 3 ;
}
else {
return - 1 ;
}
}
public static void main(String[] args)
{
long A = 4 , B = 5 , C = 0 , D = 15 , E = 5 ;
long answer = minOperations(A, B, C, D, E);
System.out.println(answer);
}
}
|
Python3
import sys
def minOperations(A, B, C, D, E):
if A = = B:
return 0
elif abs (A - B) > = E:
return 1
elif D - max (A, B) > = E or min (A, B) - C > = E:
return 2
elif (D - B > = E and A - C > = E) or (D - A > = E and B - C > = E):
return 3
else :
return - 1
if __name__ = = '__main__' :
A = 4
B = 5
C = 0
D = 15
E = 5
answer = minOperations(A, B, C, D, E)
print (answer)
|
C#
using System;
class GFG {
static int MinOperations( int A, int B, int C, int D, int E) {
if (A == B) {
return 0;
}
else if (Math.Abs(A - B) >= E) {
return 1;
}
else if (D - Math.Max(A, B) >= E || Math.Min(A, B) - C >= E) {
return 2;
}
else if ((D - B >= E && A - C >= E) || (D - A >= E && B - C >= E)) {
return 3;
}
else {
return -1;
}
}
static void Main( string [] args) {
int A = 4, B = 5, C = 0, D = 15, E = 5;
int answer = MinOperations(A, B, C, D, E);
Console.WriteLine(answer);
}
}
|
Javascript
function minOperations(A, B, C, D, E) {
if (A == B) {
return 0;
}
else if (Math.abs(A - B) >= E) {
return 1;
}
else if (D - Math.max(A, B) >= E || Math.min(A, B) - C >= E) {
return 2;
}
else if ((D - B >= E && A - C >= E) || (D - A >= E && B - C >= E)) {
return 3;
}
else {
return -1;
}
}
let A = 4, B = 5, C = 0, D = 15, E = 5;
let answer = minOperations(A, B, C, D, E);
console.log(answer);
|
Time Complexity: O(1)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...