
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Multiply the given number by 2 such that it is divisible by 10
This problem statement says that we are allowed to perform only one operation i.e. multiply the given number by 2 such that it is divisible by 10.
We will be given a number say n. The only operation that we can perform on a given number is that we can multiply the given number by 2 until it is divisible by 10. We need to determine the minimum number of operations required to make the number such that it is divisible by 10 by repeatedly multiplying the given number n by 2.
Else, print -1 if it is not possible to convert the number such that it is divisible by 10.
Example
INPUT: 25
OUTPUT: 1
Since multiplying the given number with 2 one time will give 50 which is divisible by 10.
INPUT: 20
OUTPUT: 0
The given number is already divisible by 10, so we don’t need to perform any set of operations on it.
INPUT: 2
OUTPUT: -1
Since multiplying the given number i.e 2 by 2 repeatedly never gives a number that will be divisible by 10. Thus, the output will be -1.
Algorithm
We are going to solve this problem with a simple mathematics algorithm. Below is a step-by-step illustration of the approach that we are gonna use to solve this problem.
Any number is divisible by 10 only if the unit digit of the given number is 0.
So for this problem, we will take the unit digit of the given number and check it to solve this problem.
If the last digit of the number will be 0, the least number of operations required to make the given number divisible by 10 will be 0 since it is already divisible by 10.
If the last digit of the number is 5, then the minimum number of operations required will be 1 since it will be divided by 10 after being multiplied by 2 once.
If the last digit of the number is any even number or an odd number (other than 0 or 5), multiplying it by 2 will always give an even number making it impossible to make a number divisible by 10. Thus, the output will be -1 since it is impossible to convert the given number into a number divisible by 10 by multiplying it by 2.
Approach
Approach-1 (using if-else-if)
In this approach we will use the same above algorithm using if-else-if conditional statements to get our output.
We will declare two variables, first remainder to store the unit digit of the given number and oprtn to store the number of operations we need to perform.
Then we’ll check the remainder using if-else-if statements and store the answer in oprtn according to the condition it will satisfy.
And then finally we will return oprtn which will be our desired output.
Example
Below is the C++ implementation of the above approach −
#include <iostream> #include<bits/stdc++.h> using namespace std; int numberOf(int n){ //function to calculate no of operations int remainder, oprtn; remainder= n%10; // to get the unit digit of the given number if(remainder==0){ //if the unit digit is 0 or it is divisible by 10 oprtn=0; } else if(remainder==5){ //if the unit digit is 5, no of operation required is 1 oprtn=1; } else{ //if the unit digit is ny number other than 0 or 5 oprtn=-1; } return oprtn; } int main(){ int n=4; cout<<numberOf(n)<<endl; n=15; cout<<numberOf(n)<<endl; return 0; }
Output
-1 1
Time Complexity: O(1), since constant operations are carried out.
Space Complexity: O(1).
Approach-2 (using nested if else statements)
In this approach, we will solve the problem using nested if else statements.
Check if the number is divisible by 10. If it is print 0.
If not then multiply the number by 2 and then again check if it is divisible by 10. If it is, that means the unit digit of the given number is 5 so print 1 since we require one operation to make it divisible by 10.
Else, print -1 because the unit digit is any number other than 0 or 5 and it is impossible to make the number divisible by 10 just by multiplying it by 2.
Example
Below is the C++ implementation of the above approach −
#include <iostream> #include<bits/stdc++.h> using namespace std; //Driver Code int main(){ int N=13; if(N%10==0){ //if the number is divisible by 10 cout<<0; } else{ //nested if-else N=N*2; //multiplying the number by 2 if(N%10==0){ //if number is divisible by 10 after multiplying by 2 i.e. last digit was 5 cout<<1; } else { //the unit digit is any number other than 0 or 5 cout<<-1; } } return 0; }
Output
-1
Since it is impossible to make 13 divisible by 10 just by multiplying it by 2.
Time Complexity : O(1), since constant operations are carried out.
Space Complexity : O(1).
Conclusion
In this article, we have learned to solve the problem to find out the minimum number of operations required to make a given number divisible by 10 by multiplying the given number by 2. We have tried to solve the problem with two different approaches. I hope this article helps you in understanding the problem and clear your concept about the problem.
- Related Articles
- It is given that 65610 is divisible by 27. Which two numbers nearest to 65610 are each divisible by 27?
- A number is divisible by $12$. By what other numbers will that number be divisible?
- A rational number is such that when you multiply it by \( \frac{5}{2} \) and add \( \frac{2}{3} \) to the product, you get \( -\frac{7}{12} \). What is the number?
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- Add N digits to A such that it is divisible by B after each addition?
- Which of the following statements are true?(i) If a number is divisible by 3, it must be divisible by 9.(ii) If a number is divisible by 9, it must be divisible by 3.(iii) If a number is divisible by 4, it must be divisible by 8.(iv) If a number is divisible by 8, it must be divisible by 4.(v) A number is divisible by 18, if it is divisible by both 3 and 6.(vi) If a number is divisible by both 9 and 10, it must be divisible by 90.(vii) If a number exactly divides the sum of two numbers, it must exactly divide the numbers separately.(viii) If a number divides three numbers exactly, it must divide their sum exactly.(ix) If two numbers are co-prime, at least one of them must be a prime number.(x) The sum of two consecutive odd numbers is always divisible by 4.
- If a number is divisible by 3 it must be divisible by 9. (True/False)
- If $x$ is a digit of the number $\overline{66784x}$ such that it is divisible by 9, find the possible values of $x$.
- 18 is divisible by both 2 and 3 . It is also divisible by \( 2 \times 3=6 \). Similarly, a number is divisible by both 4 and 6. Can we say that the number must also be divisible by \( 4 \times 6=24 \) ? If not, give an example to justify your answer.
- A number is divisible by both 5 and 12. By which other number will that number be always divisible?
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- 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] in C++
- Find an array element such that all elements are divisible by it using c++
- Find the least number that is divisible by all the numbers between 1 and 10 (both inclusive).
- The number 144 is divisible by the prime numbers 2 and ___
