Given a large binary string str and an integer K, the task is to find the value of str % K.
Input: str = “1101”, K = 45
decimal(1101) % 45 = 13 % 45 = 13
Input: str = “11010101”, K = 112
decimal(11010101) % 112 = 213 % 112 = 101
Approach: It is known that (str % K) where str is a binary string can be written as ((str[n – 1] * 20) + (str[n – 2] * 21) + … + (str * 2n – 1)) % K which in turn can be written as (((str[n – 1] * 20) % K) + ((str[n – 2] * 21) % K) + … + ((str * 2n – 1)) % K) % K. This can be used to find the required answer without actually converting the given binary string to its decimal equivalent.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Multiply large integers under large modulo
- Modulo power for large numbers represented as strings
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Divide large number represented as string
- Square of large number represented as String
- 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
- Largest sub-string of a binary string divisible by 2
- Print string after removing all (“10” or “01”) from the binary string
- Python | Check if a given string is binary string or not
- Binary Search a String
- Rotations of a Binary String with Odd Value
- Count of 1-bit and 2-bit characters in the given binary string
- Construct a binary string following the given constraints
- Check if all the 1's in a binary string are equidistant or not
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.