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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Method to print the minimum
// number of operations
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()
{
// Inputs
int A = 4;
int B = -5;
// Function call
Min_operations(A, B);
}
Java
// Java code to implement the approach
import java.util.*;
class Main {
// Driver Function
public static void main(String[] args)
throws java.lang.Exception
{
// Inputs
int A = 4;
int B = -5;
// Function call
Min_operations(A, B);
}
// Method to print the minimum
// number of operations
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
# Python Implementation:
# Method to print the minimum
# number of operations
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")
# Inputs
A = 4
B = -5
# Function call
Min_operations(A, B)
# This code is contributed by Sakshi
C#
using System;
public class GFG {
// Method to print the minimum
// number of operations
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");
}
}
// Driver Function
public static void Main()
{
// Inputs
int A = 4;
int B = -5;
// Function call
MinOperations(A, B);
}
}
//This code is contributed by Rohit Singh
JavaScript
// JavaScript Implementation
// Method to print the minimum number of operations
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");
}
}
// Inputs
const A = 4;
const B = -5;
// Function call
minOperations(A, B);
// This code is contributed by Tapesh(tapeshdua420)
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Minimum bitwise operations to convert given a into b. Given two positive integer a and b you have to change a to b by applying any of the three operations on binary form of a. You can select ai and aj (any two bits where i!=j) from binary form of a and then perform operation as: AND operation as : temp = ai & aj, ai = temp & ai, aj = temp &
8 min read
Minimum cost to convert one given string to another using swap, insert or delete operations Given two strings A and B of length N and M respectively, the task is to find the minimum cost to convert string A to B using the following operations: A character of string A can be swapped from another character of the same string. Cost = 0.A character can be deleted from string B or can be insert
6 min read
Minimum number of operation required to convert number x into y Given a initial number x and two operations which are given below: Multiply number by 2.Subtract 1 from the number. The task is to find out minimum number of operation required to convert number x into y using only above two operations. We can apply these operations any number of times.Constraints:
12 min read
Minimum number operations required to convert n to m | Set-2 Given two integers n and m and a and b, in a single operation n can be multiplied by either a or b. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1. Examples: Input: n = 120, m = 51840, a = 2, b = 3
5 min read
Minimize cost to convert given two integers to zero using given operations Given two integers X and Y, and two values cost1 and cost2, the task is to convert the given two numbers equal to zero at minimal cost by performing the following two types of operations: Increase or decrease any one of them by 1 at cost1.Increase or decrease both of them by 1 at cost2. Examples: In
6 min read