Given a number, the only operation allowed is to multiply the number by 2. Calculate the minimum number of operations to make the number divisible by 10.
NOTE: If it is not possible to convert then print -1.
As the given number is itself divisible by 10,
the answer is 0.
As by multiplying with 2, given no. can’t be
converted into a number that is divisible by 10,
therefore the answer is -1.
Approach: Any given number is divisible by 10 only if the last digit of the number is 0. For this problem, extract the last digit of the input number and check it in the following ways :
1) If the last digit is 0 then it is already divisible by 10 , so the minimum number of steps is 0.
2) If the last digit is 5 then multiplying it by 2 one time will make it divisible by 10, so the minimum number of steps is 1.
3) If the last digit is an even or odd number (apart from 0 and 5) then multiplying it by 2 any number of times will only produce even number so we can never make it divisible by 10. Therefore the number of steps is -1.
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.
- Queries to multiply the given subarray with given number X and print sum
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Find the smallest number whose digits multiply to a given number n
- Multiply N complex numbers given as strings
- Smallest number to multiply to convert floating point to natural
- Multiply perfect number
- Maximum splits in binary string such that each substring is divisible by given odd number
- Number of ways to split a binary number such that every part is divisible by 2
- Efficient way to multiply with 7
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Russian Peasant (Multiply two numbers using bitwise operators)
- Multiply two polynomials
- Multiply Large Numbers represented as Strings
- Multiply large integers under large modulo
- Ways to multiply n elements with an associative operation
- Multiply Large Numbers using Grid Method
- Program to multiply two matrices
- Find permutation of n which is divisible by 3 but not divisible by 6
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Find two numbers made up of a given digit such that their difference is divisible by N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.