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) 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++
#include <bits/stdc++.h>
using namespace std;
int getMod(string str, int n, int 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;
}
int res = 0;
int i = 0, j = n - 1;
while (i < n)
{
if (str[j] == '1' )
{
res += (pwrTwo[i]);
res %= k;
}
i++;
j--;
}
return res;
}
int main()
{
string str = "1101" ;
int n = str.length();
int k = 45;
cout << getMod(str, n, k) << endl;
}
|
Java
import java.util.*;
class GFG {
static int getMod(String str, int n, int 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;
}
int res = 0 ;
int i = 0 , j = n - 1 ;
while (i < n) {
if (str.charAt(j) == '1' ) {
res += (pwrTwo[i]);
res %= k;
}
i++;
j--;
}
return res;
}
public static void main(String[] args)
{
String str = "1101" ;
int n = str.length();
int k = 45 ;
System.out.print(getMod(str, n, k));
}
}
|
Python3
def getMod(_str, n, k) :
pwrTwo = [ 0 ] * n
pwrTwo[ 0 ] = 1 % k
for i in range ( 1 , n):
pwrTwo[i] = pwrTwo[i - 1 ] * ( 2 % k)
pwrTwo[i] % = k
res = 0
i = 0
j = n - 1
while (i < n) :
if (_str[j] = = '1' ) :
res + = (pwrTwo[i])
res % = k
i + = 1
j - = 1
return res
_str = "1101"
n = len (_str)
k = 45
print (getMod(_str, n, k))
|
C#
using System;
class GFG
{
static int getMod( string str, int n, int k)
{
int i;
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;
}
int res = 0;
i = 0;
int j = n - 1;
while (i < n)
{
if (str[j] == '1' )
{
res += (pwrTwo[i]);
res %= k;
}
i++;
j--;
}
return res;
}
public static void Main()
{
string str = "1101" ;
int n = str.Length;
int k = 45;
Console.Write(getMod(str, n, k));
}
}
|
Javascript
<script>
function getMod(str, n, 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;
}
var res = 0;
var i = 0, j = n - 1;
while (i < n)
{
if (str[j] == '1' )
{
res += (pwrTwo[i]);
res %= k;
}
i++;
j--;
}
return res;
}
var str = "1101" ;
var n = str.length;
var k = 45;
document.write( getMod(str, n, k));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
10 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...