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 email@example.com. 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.
- Decimal representation of given binary string is divisible by 20 or not
- Decimal representation of given binary string is divisible by 5 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
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Maximum 0's between two immediate 1's in binary representation
- Find value of k-th bit in binary representation
- Binary representation of next number
- Binary representation of a given number
- XOR counts of 0s and 1s in binary representation
- Next greater number than N with exactly one bit different in binary representation of N
- Largest number with binary representation is m 1's and m-1 0's
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Count numbers have all 1s together in binary representation
- Binary representation of previous number
Improved By : Mithun Kumar