Solving Binary String Modulo Problem
Last Updated :
17 Jan, 2024
Given a string "s" and an integer "m" your objective is to calculate the remainder "r" when the decimal value of binary string "s" is divided by "m".
Examples:
Input: s = "101", m = 2
Output: 1
Explanation: If we have a string "(101)" its decimal equivalent is "(5)". Therefore if we compute 5 mod 2 the result will be 1.
Input: s = "1000", m = 4
Output: 0
Explanation: If we have a string "(1000)" and m = 4 then r can be calculated as k mod m, which, in this case's 8 mod 4. The final result will be 0.
Approach: To solve the problem
The idea is to calculate the value of each corresponding set bit and use it right away without storing it in an array, as we know the value of every higher set bit is 2x of the previous set bit so we can simply use a variable power and at every bit we multiply power with 2 to get the value of the current ith bit (value of 2i).
Steps for Implementing the above Approach:
- Initialize the answer with 0, and power with 1.
- Now run a loop over the binary string from the back side of the string(i.e. from the least significant bit of binary string).
- Check if the current bit is set or not then add the power(2i => which is stored in the power variable) into the answer and take its modulo with m.
- Now multiply power with 2 to get the next set bit value and also take it modulo with m so that it can't overflow the integer limit.
- Return the answer.
Below is the implementation of the above idea:
C++
#include <iostream>
using namespace std;
int modulo(string s, int m)
{
int ans = 0;
int power = 1;
for (int i = s.size() - 1; i >= 0; i--) {
if (s[i] == '1') {
ans += power;
ans %= m;
}
power *= 2;
power %= m;
}
return ans;
}
int main()
{
string s = "101";
int m = 2;
int result = modulo(s, m);
cout << result << endl;
return 0;
}
Java
public class Main {
// Function to calculate modulo value
public static int modulo(String s, int m) {
int ans = 0;
int power = 1;
// Loop through the string in reverse order
for (int i = s.length() - 1; i >= 0; i--) {
// Check if the current character is '1'
if (s.charAt(i) == '1') {
ans += power;
ans %= m;
}
power *= 2;
power %= m;
}
return ans;
}
// Driver code
public static void main(String[] args) {
String s = "101";
int m = 2;
int result = modulo(s, m);
System.out.println(result);
}
}
// This code is contributed by shivamgupta310570
Python3
def modulo(s, m):
ans = 0
power = 1
# Loop through the string in reverse order
for i in range(len(s) - 1, -1, -1):
# Check if the current digit is '1'
if s[i] == '1':
ans += power
ans %= m
power *= 2
power %= m
return ans
# Driver code
def main():
s = "101"
m = 2
result = modulo(s, m)
print(result)
if __name__ == "__main__":
main()
C#
using System;
public class GFG
{
// Function to calculate modulo value
public static int Modulo(string s, int m)
{
int ans = 0;
int power = 1;
// Loop through the string in reverse order
for (int i = s.Length - 1; i >= 0; i--)
{
// Check if the current character is '1'
if (s[i] == '1')
{
ans += power;
ans %= m;
}
power *= 2;
power %= m;
}
return ans;
}
// Driver code
public static void Main(string[] args)
{
string s = "101";
int m = 2;
int result = Modulo(s, m);
Console.WriteLine(result);
}
}
JavaScript
// JavaScript Implementation
function modulo(s, m) {
let ans = 0;
let power = 1;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === '1') {
ans += power;
ans %= m;
}
power *= 2;
power %= m;
}
return ans;
}
let s = "101";
let m = 2;
let result = modulo(s, m);
console.log(result);
// This code is contributed by Sakshi
Time Complexity:- O(N), As we are only using a single loop over the size of binary string.
Auxiliary Space:- O(1), As we are not using any extra space.
Similar Reads
Modulo of a large Binary String Given a large binary string str and an integer K, the task is to find the value of str % K.Examples: Input: str = "1101", K = 45 Output: 13 decimal(1101) % 45 = 13 % 45 = 13 Input: str = "11010101", K = 112 Output: 101 decimal(11010101) % 112 = 213 % 112 = 101 Approach: It is known that (str % K) wh
5 min read
Largest sub-string of a binary string divisible by 2 Given binary string str of length N, the task is to find the longest sub-string divisible by 2. If no such sub-string exists then print -1. Examples: Input: str = "11100011" Output: 111000 Largest sub-string divisible by 2 is "111000".Input: str = "1111" Output: -1 There is no sub-string of the give
3 min read
Modulo power for large numbers represented as strings Given two numbers sa and sb represented as strings, find ab % MOD where MOD is 1e9 + 7. The numbers a and b can contain upto 106 digits.Examples: Input : sa = 2, sb = 3 Output : 8Input : sa = 10000000000000000000000000000000000000000000 sb = 10000000000000000000000000000000000000000000 Output : 4942
9 min read
Find the Longest Common Substring using Binary search and Rolling Hash Given two strings X and Y, the task is to find the length of the longest common substring. Examples: Input: X = âGeeksforGeeksâ, y = âGeeksQuizâ Output: 5 Explanation: The longest common substring is âGeeksâ and is of length 5. Input: X = âabcdxyzâ, y = âxyzabcdâ Output: 4 Explanation: The longest c
11 min read
CSES Solutions - Creating Strings II Given a string, your task is to calculate the number of different strings that can be created using its characters. Examples: Input: S = "aa"Output: 1Explanation: There is only 1 possible string: "aa". Input: S = "aabac"Output: 20Explanation: There are 20 possible strings: "aaabc", "aaacb", "aabac",
5 min read