-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryModulo.cpp
42 lines (36 loc) · 966 Bytes
/
BinaryModulo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
You are given a binary string s and an integer m. You need to return an integer r. Where r = k%m, k is the binary equivalent of string s.
Example 1:
Input:
s = "101"
m = 2
Output:
1
Explanation: Here k=5 because (101)2 = (5)10.
Hence 5 mod 2 = 1.
Example 2:
Input:
s = "1000"
m = 4
Output:
0
Explanation: Here k=8 and m=4 hence
r = k mod m = 8 mod 4 = 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function modulo() which takes the string s and integer m as input parameters and returns the value of r as described above.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
*/
//User function Template for C++
class Solution{
public:
int modulo(string s,int m)
{
int n=s.size();
int num=0;
for(int i=0;i<n;i++){
num=((num<<1)+(s[i]-'0'))%m;
}
return num;
}
};