The problem is to check whether the decimal representation of the given binary number is divisible by 10 or not. Take care, the number could be very large and may not fit even in long long int. The approach should be such that there are zero or minimum number of multiplication and division operations. No leading 0’s are there in the input.
Input : 101000 Output : Yes (101000)2 = (40)10 and 40 is divisible by 10. Input : 11000111001110 Output : Yes
Approach: First of all we need to know that last digit of pow(2, i) = 2, 4, 8, 6 if i % 4 is equal to 1, 2, 3, 0 respectively, where i is greater than equal to 1. So, in the binary representation we need to know the position of digit ‘1’ from the right, so as to know the perfect power of 2 with which it is going to be multiplied. This will help us to obtain the last digit of the required perfect power’s of 2. We can add these digits and then check whether the last digit of the sum is 0 or not which implies that the number is divisible by 10 or not. Note that if the last digit in the binary representation is ‘1’ then it represents an odd number, and thus not divisible by 10.
Time Complexity: O(n), where n is the number of digits in the binary number.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Decimal representation of given binary string is divisible by 5 or not
- Decimal representation of given binary string is divisible by 20 or not
- Check if Decimal representation of an Octal number is divisible by 7
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Convert a given Decimal number to its BCD representation
- Number of sub-strings in a given binary string divisible by 2
- Minimum swaps required to make a binary string divisible by 2^k
- Maximum splits in binary string such that each substring is divisible by given odd number
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Binary representation of a given number
- Binary representation of next number
- Find value of k-th bit in binary representation
- XOR counts of 0s and 1s in binary representation
- Maximum 0's between two immediate 1's in binary representation
- Binary representation of previous number
- Count numbers have all 1s together in binary representation
- 1 to n bit numbers with no consecutive 1s in binary representation
- Largest number with binary representation is m 1's and m-1 0's
- Next greater number than N with exactly one bit different in binary representation of N
- Maximum distance between two 1's in Binary representation of N
Improved By : Mithun Kumar