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++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int A, int B)
{
int diff;
if (A == B)
return 0;
else if (A < B) {
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
int main()
{
int A, B;
A = 7;
B = 4;
int ans = minOperations(A, B);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minOperations( int A, int B)
{
int diff;
if (A == B)
return 0 ;
else if (A < B) {
diff = B - A;
if (diff % 2 != 0 )
return 1 ;
return 2 ;
}
else {
diff = A - B;
if (diff % 2 == 0 )
return 1 ;
return 2 ;
}
}
public static void main(String[] args)
{
int A, B;
A = 7 ;
B = 4 ;
int ans = minOperations(A, B);
System.out.print(ans);
}
}
|
Python3
def minOperations(A, B):
diff = None
if (A = = B):
return 0 ;
elif (A < B):
diff = B - A;
if (diff % 2 ! = 0 ):
return 1 ;
return 2 ;
else :
diff = A - B;
if (diff % 2 = = 0 ):
return 1 ;
return 2 ;
A = 7 ;
B = 4 ;
ans = minOperations(A, B);
print (ans);
|
C#
using System;
class GFG{
static int minOperations( int A, int B)
{
int diff;
if (A == B)
return 0;
else if (A < B) {
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
public static void Main()
{
int A, B;
A = 7;
B = 4;
int ans = minOperations(A, B);
Console.Write(ans);
}
}
|
Javascript
<script>
function minOperations(A, B)
{
let diff;
if (A == B)
return 0;
else if (A < B) {
diff = B - A;
if (diff % 2 != 0)
return 1;
return 2;
}
else {
diff = A - B;
if (diff % 2 == 0)
return 1;
return 2;
}
}
let A, B;
A = 7;
B = 4;
let ans = minOperations(A, B);
document.write(ans);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...