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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Compute n! under modulo p
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Cyclic Redundancy Check and Modulo-2 Division
- Find sum of modulo K of first N natural number
- Primitive root of a prime number n modulo n
- Maximum subarray sum modulo m
- Modulo 10^9+7 (1000000007)
- Program to find remainder without using modulo or % operator
- Fibonacci modulo p
- Maximum modulo of all the pairs of array where arr[i] >= arr[j]
- Discrete logarithm (Find an integer k such that a^k is congruent modulo b)
- Exponential Squaring (Fast Modulo Multiplication)
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.