Open In App

Minimize operations to convert A to B by adding any odd integer or subtracting any even integer

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:

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.

Hence, the number of operations will always be either 0, 1, or 2.

Below is the implementation for the above approach.




// 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 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




# 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# 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.




<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>

 
 

Output
2

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 


Article Tags :