Given a binary string and a number k, task is to check whether the binary string is evenly divisible by 2k or not.
Input : 11000 k = 2 Output : Yes Explanation : (11000)2 = (24)10 24 is evenly divisible by 2k i.e. 4. Input : 10101 k = 3 Output : No Explanation : (10101)2 = (21)10 21 is not evenly divisible by 2k i.e. 8.
Naive Approach : Compute the binary string into decimal and then simply check whether it is divisible by 2k. But, this approach is not feasible for long binary string as time complexity will be high for computing decimal number from binary string and then dividing it by 2k.
Efficient Approach : In the binary string, check for last k bits. If the all the last k bits are 0, then the binary number is evenly divisible by 2k else it is not evenly divisible. Time complexity using this approach is O(k).
Below is the implementation of the approach.
Time Complexity: O(k)
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.
- Check divisibility in a binary stream
- To check divisibility of any large number by 999
- Check the divisibility of Hexadecimal numbers
- Python | Check if a given string is binary string or not
- Check if a binary string contains consecutive same or not
- Check if all the 1's in a binary string are equidistant or not
- Check if binary string multiple of 3 using DFA
- Check if a binary string contains all permutations of length k
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if an encoding represents a unique binary string
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check whether the binary equivalent of a number ends with given string or not
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- String obtained by reversing and complementing a Binary string K times
- Longest sub string of 0's in a binary string which is repeated K times
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.