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) |
2
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!