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.
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.
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 which is the required answer.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live