Minimum decrements to make integer A divisible by integer B

Given two positive integers A and B where A is greater than B. In one move one can decrease A by 1 which implies that after one move A is equal to A – 1. The task is to find the minimum number of moves required to make A divisible by B in constant time.
Examples:
Input: A = 10, B = 3
Output: 1
Explanation: Only one move is required A = A – 1 = 9, which is divisible by 3.Input : A = 10, B = 10
Output : 0
Explanation: Since A is equal to B therefore zero move required.
Approach:
To solve the problem mentioned above we take the modulus of the numbers that are A % B and the result is stored in a variable that is the required answer.
Below is the implementation of the above approach:
C++
// C++ implementation to count// Total numbers moves to make// integer A divisible by integer B#include <bits/stdc++.h>using namespace std;// Function that print number// of moves requiredvoid movesRequired(int a, int b){ // calculate modulo int total_moves = a % b; // print the required answer cout << total_moves << "\n";}// Driver Codeint main(){ // initialise A and B int A = 10, B = 3; movesRequired(A, B); return 0;} |
Java
// Java implementation to count// total numbers moves to make// integer A divisible by integer Bimport java.util.*;class GFG{// Function that print number// of moves requiredstatic void movesRequired(int a, int b){ // Calculate modulo int total_moves = a % b; // Print the required answer System.out.println(total_moves);}// Driver codepublic static void main(String[] args){ // Initialise A and B int A = 10, B = 3; movesRequired(A, B);}}// This code is contributed by offbeat |
Python3
# Python3 implementation to count# total numbers moves to make# integer A divisible by integer B# Function that print number# of moves requireddef movesRequired(a, b): # Calculate modulo total_moves = a % b # Print the required answer print(total_moves)# Driver Codeif __name__ == '__main__': # Initialise A and B A = 10 B = 3 movesRequired(A, B)# This code is contributed by Samarth |
C#
// C# implementation to count// total numbers moves to make// integer A divisible by integer Busing System;class GFG{// Function that print number// of moves requiredstatic void movesRequired(int a, int b){ // Calculate modulo int total_moves = a % b; // Print the required answer Console.Write(total_moves);}// Driver codepublic static void Main(String []args){ // Initialise A and B int A = 10, B = 3; movesRequired(A, B);}}// This code is contributed by shivanisinghss2110 |
Javascript
<script> // Javascript implementation to count // Total numbers moves to make // integer A divisible by integer B // Function that print number // of moves required function movesRequired(a, b) { // calculate modulo let total_moves = a % b; // print the required answer document.write(total_moves); } // initialise A and B let A = 10, B = 3; movesRequired(A, B); // This code is contributed by divyesh072019.</script> |
1
Time Complexity: O(1), as constant operations are being performed.
Auxiliary Space: O(1), constant space is required for the above approach.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



