The Wayback Machine - https://web.archive.org/web/20240709113424/https://www.geeksforgeeks.org/modulo-of-a-large-binary-string/
Open In App

Modulo of a large Binary String

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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) where str is a binary string can be written as ((str[n – 1] * 20) + (str[n – 2] * 21) + … + (str[0] * 2n – 1)) % K which in turn can be written as (((str[n – 1] * 20) % K) + ((str[n – 2] * 21) % K) + … + ((str[0] * 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: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the value of (str % k)
int getMod(string str, int n, int k)
{
 
    // pwrTwo[i] will store ((2^i) % k)
    int pwrTwo[n];
    pwrTwo[0] = 1 % k;
    for (int i = 1; i < n; i++)
    {
        pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
        pwrTwo[i] %= k;
    }
 
    // To store the result
    int res = 0;
    int i = 0, j = n - 1;
    while (i < n)
    {
 
        // If current bit is 1
        if (str[j] == '1')
        {
 
            // Add the current power of 2
            res += (pwrTwo[i]);
            res %= k;
        }
        i++;
        j--;
    }
    return res;
}
 
// Driver code
int main()
{
    string str = "1101";
    int n = str.length();
    int k = 45;
 
    cout << getMod(str, n, k) << endl;
}
 
// This code is contributed by ashutosh450


Java




// Java implementation of the approach
import java.util.*;
 
class GFG {
 
    // Function to return the value of (str % k)
    static int getMod(String str, int n, int k)
    {
 
        // pwrTwo[i] will store ((2^i) % k)
        int pwrTwo[] = new int[n];
        pwrTwo[0] = 1 % k;
        for (int i = 1; i < n; i++) {
            pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
            pwrTwo[i] %= k;
        }
 
        // To store the result
        int res = 0;
        int i = 0, j = n - 1;
        while (i < n) {
 
            // If current bit is 1
            if (str.charAt(j) == '1') {
 
                // Add the current power of 2
                res += (pwrTwo[i]);
                res %= k;
            }
            i++;
            j--;
        }
 
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "1101";
        int n = str.length();
        int k = 45;
 
        System.out.print(getMod(str, n, k));
    }
}


Python3




# Python3 implementation of the approach
 
# Function to return the value of (str % k)
def getMod(_str, n, k) :
 
    # pwrTwo[i] will store ((2^i) % k)
    pwrTwo = [0] * n
    pwrTwo[0] = 1 % k
    for i in range(1, n):
        pwrTwo[i] = pwrTwo[i - 1] * (2 % k)
        pwrTwo[i] %= k
 
    # To store the result
    res = 0
    i = 0
    j = n - 1
    while (i < n) :
 
        # If current bit is 1
        if (_str[j] == '1') :
 
            # Add the current power of 2
            res += (pwrTwo[i])
            res %= k
             
        i += 1
        j -= 1
 
    return res
 
# Driver code
_str = "1101"
n = len(_str)
k = 45
 
print(getMod(_str, n, k))
 
# This code is contributed by
# divyamohan123


C#




// C# implementation of the above approach
using System;
 
class GFG
{
     
    // Function to return the value of (str % k)
    static int getMod(string str, int n, int k)
    {
        int i;
         
        // pwrTwo[i] will store ((2^i) % k)
        int []pwrTwo = new int[n];
         
        pwrTwo[0] = 1 % k;
         
        for (i = 1; i < n; i++)
        {
            pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
            pwrTwo[i] %= k;
        }
 
        // To store the result
        int res = 0;
        i = 0;
        int j = n - 1;
        while (i < n)
        {
 
            // If current bit is 1
            if (str[j] == '1')
            {
 
                // Add the current power of 2
                res += (pwrTwo[i]);
                res %= k;
            }
            i++;
            j--;
        }
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        string str = "1101";
        int n = str.Length;
        int k = 45;
 
        Console.Write(getMod(str, n, k));
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the value of (str % k)
function getMod(str, n, k)
{
 
    // pwrTwo[i] will store ((2^i) % k)
    var pwrTwo = Array(n);
    pwrTwo[0] = 1 % k;
    for (var i = 1; i < n; i++)
    {
        pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
        pwrTwo[i] %= k;
    }
 
    // To store the result
    var res = 0;
    var i = 0, j = n - 1;
    while (i < n)
    {
 
        // If current bit is 1
        if (str[j] == '1')
        {
 
            // Add the current power of 2
            res += (pwrTwo[i]);
            res %= k;
        }
        i++;
        j--;
    }
    return res;
}
 
// Driver code
var str = "1101";
var n = str.length;
var k = 45;
document.write( getMod(str, n, k));
 
 
</script>


Output: 

13

 

Time Complexity:  O(n)
Auxiliary Space: O(n)



Previous Article
Next Article

Similar Reads

Multiply large integers under large modulo
Given an integer a, b, m. Find (a * b ) mod m, where a, b may be large and their direct multiplication may cause overflow. However, they are smaller than half of the maximum allowed long long int value. Examples: Input: a = 426, b = 964, m = 235Output: 119Explanation: (426 * 964) % 235 = 410664 % 235 = 119 Input: a = 10123465234878998, b = 65746311
7 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 : 494234546 As a and b very large (may contain upto 10^6
9 min read
Number of factors of very large number N modulo M where M is any prime number
Given a large number N, the task is to find the total number of factors of the number N modulo M where M is any prime number. Examples: Input: N = 9699690, M = 17 Output: 1 Explanation: Total Number of factors of 9699690 is 256 and (256 % 17) = 1Input: N = 193748576239475639, M = 9 Output: 8 Explanation: Total Number of factors of 9699690 is 256 an
8 min read
Solving Binary String Modulo Problem
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 = 2Output: 1Explanation: 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 = 4Output
4 min read
XOR of very large Binary Numbers in range [L, R]
Given two binary strings L and R, the task is to find the xor from L to R. Length of the binary string is &lt; = 10e6. Examples: Input: L = 10, R = 100Output: 101Explanation: L = 2 and R = 4 in decimal system.Therefore, the xor will be 2^3^4 = 5(101). Input: L = 10, R = 10000Output: 10001 Naive Approach: The simplest approach to solve the problem i
15+ min read
Divide large number represented as string
Given a large number (represented as a string) which has to divide by another number (represented as int data type). The large number can be very large which does not even fit in long long in C++. The task is to find the division of these numbers. Examples:  Input : number = 1260257 divisor = 37Output : 34061(See below diagram)Input : number = 1231
7 min read
Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
Periodic Binary String: A Binary string is called periodic if it can be written as a repetition of a binary string of smaller or same length. For example, 101010 is a periodic binary string with period 10 as we can get the string by repeatedly appending 10 to itself. In general, the string S with period P means, Si is equal to Si + P. Problem: Give
5 min read
Segment Trees | (Product of given Range Modulo m)
Let us consider the following problem to understand Segment Trees.We have an array arr[0 . . . n-1]. We should be able to 1 Find the product of elements from index l to r where 0 &lt;= l &lt;= r &lt;= n-1 take its modulus by an integer m.2 Change value of a specified element of the array to a new value x. We need to do arr[i] = x where 0 &lt;= i
15+ min read
Equalizing array using increment under modulo 3
Given an array A containing N non-negative integers. Only following operation can be performed on the array: A[i] = ( A[i] + 1 ) % 3 where A[i] is the element of array A at index i, and performing this operation once costs 1 unit. Find the minimum cost to make all elements equal. Examples : Input : 1 0 3 2 Output : 4 units Explanation: First 3 is c
14 min read
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
Given K, number of digits, and d0 and d1 as the two digits to form the k-sized integer. Task is to check whether the k-sized number formed using d0 and d1 is divisible by 3 or not. For each i, di is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold : [Tex]\huge{d_{i} = \sum_{j = 0}^{i - 1
11 min read
three90RightbarBannerImg