Given a binary string S, the task is to find that the decimal representation of the given binary string is divisible by integer K or not.
Examples:
Input: S = 1010, k = 5
Output: Yes
Explanation: Decimal representation of 1010 (=10) is divisible by 5
Input: S = 1010, k = 6
Output: No
Approach: Since the modulo operator is distributive over addition, the given binary string can be checked bit by bit to see whether the decimal % k is equal to zero or not. Follow the steps below for the approach:
- Initialize an array poweroftwo[], of size of binary string, to store the powers of two.
- Iterate till size and for each i store 2i % K in poweroftwo[].
- Initialize variables, say rem = 0, to store the current remaining number till i.
- Iterate till size and for each i and if S[size – i -1] is 1 then update rem equals rem + poweroftwo[i].
- Finally, return Yes if rem equals zero else return No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string divisibleByk(string s, int n, int k)
{
int poweroftwo[n];
poweroftwo[0] = 1 % k;
for ( int i = 1; i < n; i++) {
poweroftwo[i] = (poweroftwo[i - 1]
* (2 % k))
% k;
}
int rem = 0;
for ( int i = 0; i < n; i++) {
if (s[n - i - 1] == '1' ) {
rem += (poweroftwo[i]);
rem %= k;
}
}
if (rem == 0) {
return "Yes" ;
}
else
return "No" ;
}
int main()
{
string s = "1010001" ;
int k = 9;
int n = s.length();
cout << divisibleByk(s, n, k);
return 0;
}
|
Java
class GFG
{
public static String divisibleByk(String s, int n, int k) {
int [] poweroftwo = new int [n];
poweroftwo[ 0 ] = 1 % k;
for ( int i = 1 ; i < n; i++) {
poweroftwo[i] = (poweroftwo[i - 1 ] * ( 2 % k)) % k;
}
int rem = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(n - i - 1 ) == '1' ) {
rem += (poweroftwo[i]);
rem %= k;
}
}
if (rem == 0 ) {
return "Yes" ;
}
else
return "No" ;
}
public static void main(String args[])
{
String s = "1010001" ;
int k = 9 ;
int n = s.length();
System.out.println(divisibleByk(s, n, k));
}
}
|
Python3
def divisibleByk(s, n, k):
poweroftwo = [ 0 for i in range (n)]
poweroftwo[ 0 ] = 1 % k
for i in range ( 1 ,n, 1 ):
poweroftwo[i] = (poweroftwo[i - 1 ] * ( 2 % k)) % k
rem = 0
for i in range (n):
if (s[n - i - 1 ] = = '1' ):
rem + = (poweroftwo[i])
rem % = k
if (rem = = 0 ):
return "Yes"
else :
return "No"
if __name__ = = '__main__' :
s = "1010001"
k = 9
n = len (s)
print (divisibleByk(s, n, k))
|
C#
using System;
class GFG
{
public static String divisibleByk(String s, int n, int k) {
int [] poweroftwo = new int [n];
poweroftwo[0] = 1 % k;
for ( int i = 1; i < n; i++) {
poweroftwo[i] = (poweroftwo[i - 1] * (2 % k)) % k;
}
int rem = 0;
for ( int i = 0; i < n; i++) {
if (s[n - i - 1] == '1' ) {
rem += (poweroftwo[i]);
rem %= k;
}
}
if (rem == 0) {
return "Yes" ;
}
else
return "No" ;
}
public static void Main(String []args)
{
String s = "1010001" ;
int k = 9;
int n = s.Length;
Console.Write(divisibleByk(s, n, k));
}
}
|
Javascript
<script>
function divisibleByk(s, n, k)
{
let poweroftwo = new Array(n);
poweroftwo[0] = 1 % k;
for (let i = 1; i < n; i++) {
poweroftwo[i] = (poweroftwo[i - 1]
* (2 % k))
% k;
}
let rem = 0;
for (let i = 0; i < n; i++) {
if (s[n - i - 1] == '1' ) {
rem += (poweroftwo[i]);
rem %= k;
}
}
if (rem == 0) {
return "Yes" ;
}
else
return "No" ;
}
let s = "1010001" ;
let k = 9;
let n = s.length;
document.write(divisibleByk(s, n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)