Minimum operations required to convert A into B using given conditions
Last Updated :
29 Nov, 2023
Given two integers A and B, you have to choose two integers X and Y such that X must be odd, and Y must be even. Then you have to make A equal to B by using one out of two operations below at a time (possible zero time), the task is to find the minimum number of operations required to make integer A equal to B.
- Add X into A.
- Subtract Y from A.
Examples:
Input: A = 4, B = -5
Output: 2
Explanation: Two chosen integers are X = 1 and Y = 10, which are odd and even respectively.
- First operation: Add 1 into A, Then A = 4+1 = 5
- Second operation: Subtract 10 from A, then A = -10
Now A is equal to B. It can be verified that any other value of X and Y can’t make A into B in less than 2 operations.
Input: A = 10, B = -12
Output: 1
Explanation: It can be verified that minimum operations will be 1, If we chose X and Y optimally.
Approach: Implement the idea below to solve the problem:
The problem is based on the observation. We can divide the following problem into sub-parts and solve it:
- If (A < B)
- If difference between A and B is odd, then operations required will be 1.
- If difference is multiple of 4, then operations required will be 3.
- Otherwise, the minimum operations will be 2.
- If (A > B)
- If difference between A and B is odd, then required operations will be 2.
- Otherwise only 1 operation is required.
- If (A == B)
- A is already equal to B, thus no operation is required. Answer will be 0.
Steps to solve the problem:
- If (A < B)
- If ((B-A)%2 != 0), then minimum operations will be 1.
- Else If ((B-A)%4 == 0), then minimum operations will be 3.
- Else, then minimum operations will be 2.
- Else if (A > B)
- If((A-B)%2 == 1), then minimum operations will be 2.
- Else, minimum operation will be 1.
- Else If (A == B), then minimum operation is 0.
Below is the implementation of the above approah:
C++
#include <bits/stdc++.h>
using namespace std;
void Min_operations( int A, int B)
{
if (A < B) {
if ((B - A) % 2 != 0)
cout << "1" << endl;
else if ((B - A) % 4 == 0)
cout << "3" << endl;
else
cout << "2" << endl;
}
else if (A > B) {
if ((A - B) % 2 == 1)
cout << "2" << endl;
else
cout << "1" << endl;
}
else {
cout << "0" << endl;
}
}
int main()
{
int A = 4;
int B = -5;
Min_operations(A, B);
}
|
Java
import java.util.*;
class Main {
public static void main(String[] args)
throws java.lang.Exception
{
int A = 4 ;
int B = - 5 ;
Min_operations(A, B);
}
public static void Min_operations( int A, int B)
{
if (A < B) {
if ((B - A) % 2 != 0 )
System.out.println( 1 );
else if ((B - A) % 4 == 0 )
System.out.println( 3 );
else
System.out.println( 2 );
}
else if (A > B) {
if ((A - B) % 2 == 1 )
System.out.println( 2 );
else
System.out.println( 1 );
}
else {
System.out.println( 0 );
}
}
}
|
Python3
def Min_operations(A, B):
if A < B:
if (B - A) % 2 ! = 0 :
print ( "1" )
elif (B - A) % 4 = = 0 :
print ( "3" )
else :
print ( "2" )
elif A > B:
if (A - B) % 2 = = 1 :
print ( "2" )
else :
print ( "1" )
else :
print ( "0" )
A = 4
B = - 5
Min_operations(A, B)
|
C#
using System;
public class GFG {
public static void MinOperations( int A, int B)
{
if (A < B) {
if ((B - A) % 2 != 0) {
Console.WriteLine( "1" );
}
else if ((B - A) % 4 == 0) {
Console.WriteLine( "3" );
}
else {
Console.WriteLine( "2" );
}
}
else if (A > B) {
if ((A - B) % 2 == 1) {
Console.WriteLine( "2" );
}
else {
Console.WriteLine( "1" );
}
}
else {
Console.WriteLine( "0" );
}
}
public static void Main()
{
int A = 4;
int B = -5;
MinOperations(A, B);
}
}
|
Javascript
function minOperations(A, B) {
if (A < B) {
if ((B - A) % 2 !== 0) {
console.log( "1" );
} else if ((B - A) % 4 === 0) {
console.log( "3" );
} else {
console.log( "2" );
}
} else if (A > B) {
if ((A - B) % 2 === 1) {
console.log( "2" );
} else {
console.log( "1" );
}
} else {
console.log( "0" );
}
}
const A = 4;
const B = -5;
minOperations(A, B);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...