Minimize operations to convert A to B by adding any odd integer or subtracting any even integer
Last Updated :
31 Jan, 2022
Given two positive integers A and B. The task is to find the minimum number of operations required to convert number A into B. In a move, any one of the following operations can be applied on the number A:
- Select any odd integer x (x>0) and add it to A i.e. (A+x);
- Or, select any even integer y (y>0) and subtract it from A i.e. (A-y).
Examples:
Input: A = 2, B = 3
Output: 1
Explanation: Add odd x = 1 to A to obtain B (1 + 2 = 3).
Input: A = 7, B = 4
Output: 2
Explanation: Two operations are required:
Subtract y = 4 from A (7 - 4 = 3).
Add x = 1 to get B (3 + 1 = 4).
Approach: This is an implementation-based problem. Follow the steps below to solve the given problem.
- Store absolute difference of A-B in variable diff.
- Check if A is equal to B. As the two integers are equal, the total number of operations will be 0.
- Else check if A < B.
- If yes, check if their difference is odd or even.
- If diff is odd, A+x operation is applied once (x is an odd integer equal to diff). The total number of operations is 1.
- Else diff is even, apply A+x operation twice, Firstly where x is diff -1 (odd) and secondly where x is 1. Or, A+x operation can be followed by A-y operation. In either case, the total number of operations will be 2.
- Else if A> B, the opposite set of operations are applied i.e.
- If diff is even, A-y operation is applied once.
- Else A-y operation can be applied followed by A+x operation. Or, A-y operation can be applied twice.
Hence, the number of operations will always be either 0, 1, or 2.
Below is the implementation for the above approach.
C++
// C++ program for the given approach
#include <bits/stdc++.h>
using namespace std;
// Function to find
// minimum number of operations
int minOperations(int A, int B)
{
// Variable to store
// difference of A and B
int diff;
if (A == B)
return 0;
else if (A < B) {
// A+x operation first
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
// A-y operation first
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
// Driver code
int main()
{
// Declaring integers A and B
int A, B;
// Initialising
A = 7;
B = 4;
// Function call
int ans = minOperations(A, B);
// Displaying the result
cout << ans;
return 0;
}
Java
// Java program for the given approach
import java.util.*;
class GFG{
// Function to find
// minimum number of operations
static int minOperations(int A, int B)
{
// Variable to store
// difference of A and B
int diff;
if (A == B)
return 0;
else if (A < B) {
// A+x operation first
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
// A-y operation first
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
// Driver code
public static void main(String[] args)
{
// Declaring integers A and B
int A, B;
// Initialising
A = 7;
B = 4;
// Function call
int ans = minOperations(A, B);
// Displaying the result
System.out.print(ans);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code for the above approach
# Function to find
# minimum number of operations
def minOperations(A, B):
# Variable to store
# difference of A and B
diff = None
if (A == B):
return 0;
elif (A < B):
# A+x operation first
diff = B - A;
if (diff % 2 != 0):
return 1;
return 2;
else:
# A-y operation first
diff = A - B;
if (diff % 2 == 0):
return 1;
return 2;
# Driver code
# Initialising A and B
A = 7;
B = 4;
# Function call
ans = minOperations(A, B);
# Displaying the result
print(ans);
# This code is contributed by gfgking
C#
// C# program for the given approach
using System;
class GFG{
// Function to find
// minimum number of operations
static int minOperations(int A, int B)
{
// Variable to store
// difference of A and B
int diff;
if (A == B)
return 0;
else if (A < B) {
// A+x operation first
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
// A-y operation first
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
// Driver code
public static void Main()
{
// Declaring integers A and B
int A, B;
// Initialising
A = 7;
B = 4;
// Function call
int ans = minOperations(A, B);
// Displaying the result
Console.Write(ans);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find
// minimum number of operations
function minOperations(A, B)
{
// Variable to store
// difference of A and B
let diff;
if (A == B)
return 0;
else if (A < B) {
// A+x operation first
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
// A-y operation first
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
// Driver code
// Declaring integers A and B
let A, B;
// Initialising
A = 7;
B = 4;
// Function call
let ans = minOperations(A, B);
// Displaying the result
document.write(ans);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Minimize operations to convert K from 0 to B by adding 1 or A * 10^c in each step Given three numbers A, B, and K where K is 0 initially. The task is to find the minimum operations to convert K into B using the below operations: Add 1 to K i.e K = K + 1Add A * 10c in K i.e K = K + A * 10c, where c is any integer (c >= 0). Examples: Input: A = 2, B = 7Output: 4Explanation: Init
7 min read
Minimize operations to convert (0, 0) to (N, M) by incrementing either or both by K Given two integers N and M, the task is to calculate the minimum number of operations required to convert (0, 0) to (N, M) using the following operations: Choose any integer K and convert (x, y) to (x + K, y + K).Choose any integer K and convert (x, y) to (x - K, y + K) or (x + K, y - K). Examples:
5 min read
Minimize operations to reduce A and B to 1 by decrementing 1 or dividing A by B and B by A Given two positive integers A and B. The task is to minimize operations required to reduce A and B to 1. There are two types of operation: Decrement either A or B by 1.Divide A by B or B by A or perform both divisions simultaneously only if the remainder on division is 0. Example: Input: A = 13, B =
8 min read
Minimize adding odd and subtracting even numbers to make all array elements equal to K Given an array, arr[] of size N and an integer K, the task is to find the minimum number of operations required to make all array elements equal to K by performing the following operations any number of times: Convert arr[i] to arr[i] + X, where X is an odd number.Convert arr[i] to arr[i] - Y, where
7 min read
Minimize operations to reduce A or B to 0 by reducing A from B or B from A Given two numbers A and B, the task is to find the minimum number of operations required to reduce either A or B to 0, wherein each operation A can be reduced by B if A is greater than equal to B, or vice versa. Examples: Input: A = 5, B = 4Output: 5Explanation:Reduce B from A: A=1, B=4Reduce A from
3 min read