Open In App

Convert A into B by incrementing or decrementing 1, 2, or 5 any number of times

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers A and B, the task is to find the minimum number of moves needed to make A equal to B by incrementing or decrementing the A by either 1, 2, or 5 any number of times.

Examples:

Input: A = 4, B = 0
Output: 2
Explanation:
Perform the operation as follows:

  1. Decreasing the value of A by 2, modifies the value of A to (4 – 2) = 2.
  2. Decreasing the value of A by 2 modifies the value of A to (2 – 2) = 0. Which is equal to B.

Therefore, the number of moves required is 2.

Input: A = 3, B = 9
Output: 2

Approach: The given problem can be solved by using the Greedy Approach. The idea is to first find the increment or decrements of 5, then 2, and then 1 is needed to convert A to B. Follow the steps below to solve the problem: 

  • Update the value of A as the absolute difference between A and B.
  • Now, print the value of (A/5) + (A%5)/2 + (A%5)%2 as the minimum number of increments or decrements of 1, 2, or 5 to convert A into B.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum number of
// moves required to convert A into B
int minimumSteps(int a, int b)
{
    // Stores the minimum number of
    // moves required
    int cnt = 0;
 
    // Stores the absolute
    // difference
    a = abs(a - b);
 
    // FInd the number of moves
    cnt = (a / 5) + (a % 5) / 2 + (a % 5) % 2;
 
    // Return cnt
    return cnt;
}
 
// Driver Code
int main()
{
    // Input
    int A = 3, B = 9;
    // Function call
    cout << minimumSteps(A, B);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find minimum number of
    // moves required to convert A into B
    static int minimumSteps(int a, int b)
    {
       
        // Stores the minimum number of
        // moves required
        int cnt = 0;
 
        // Stores the absolute
        // difference
        a = Math.abs(a - b);
 
        // FInd the number of moves
        cnt = (a / 5) + (a % 5) / 2 + (a % 5) % 2;
 
        // Return cnt
        return cnt;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int A = 3, B = 9;
        // Function call
        System.out.println(minimumSteps(A, B));
    }
}
 
 // This code is contributed by Potta Lokesh


Python3




# python program for the above approach
 
# Function to find minimum number of
# moves required to convert A into B
def minimumSteps(a, b):
   
    # Stores the minimum number of
    # moves required
    cnt = 0
 
    # Stores the absolute
    # difference
    a = abs(a - b)
 
    # FInd the number of moves
    cnt = (a//5) + (a % 5)//2 + (a % 5) % 2
     
    # Return cnt
    return cnt
 
 
# Driver Code
# Input
A = 3
B = 9
 
# Function call
print(minimumSteps(A, B))
 
# This code is contributed by amreshkumar3.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find minimum number of
// moves required to convert A into B
static int minimumSteps(int a, int b)
{
     
    // Stores the minimum number of
    // moves required
    int cnt = 0;
 
    // Stores the absolute
    // difference
    a = Math.Abs(a - b);
 
    // FInd the number of moves
    cnt = (a / 5) + (a % 5) / 2 + (a % 5) % 2;
 
    // Return cnt
    return cnt;
}
 
// Driver Code
public static void Main()
{
     
    // Input
    int A = 3, B = 9;
     
    // Function call
    Console.Write(minimumSteps(A, B));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript




<script>
        // JavaScript program for the above approach
 
        // Function to find minimum number of
        // moves required to convert A into B
        function minimumSteps(a, b)
        {
         
            // Stores the minimum number of
            // moves required
            let cnt = 0;
 
            // Stores the absolute
            // difference
            a = Math.abs(a - b);
 
            // FInd the number of moves
            cnt = Math.floor(a / 5) + Math.floor((a % 5) / 2) + (a % 5) % 2;
 
            // Return cnt
            return cnt;
        }
 
        // Driver Code
 
        // Input
        let A = 3, B = 9;
        // Function call
        document.write(minimumSteps(A, B));
 
 
    // This code is contributed by Potta Lokesh
 
    </script>


Output

2

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

 



Last Updated : 23 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads