Check if decimal representation of Binary String is divisible by 9 or not
Last Updated :
26 Apr, 2023
Given a binary string S of length N, the task is to check if the decimal representation of the binary string is divisible by 9 or not.
Examples:
Input: S = 1010001
Output:Yes
Explanation: The decimal representation of the binary string S is 81, which is divisible by 9. Therefore, the required output is Yes.
Input: S = 1010011
Output: No
Explanation: The decimal representation of the binary string S is 83, which is not divisible by 9. Therefore, the required output is No.
Naive Approach: The simplest approach to solve this problem is to convert the binary number into a decimal number and check if the decimal number is divisible by 9 or not. If found to be true then print True. Otherwise, print False.
C++
#include<iostream>
#include<bitset>
using namespace std;
void is_binary_divisible_by_9(string s){
bitset<32> decimal_num(s);
if (decimal_num.to_ulong() % 9 == 0){
cout << "Yes\n" ;
} else {
cout << "No\n" ;
}
}
int main() {
string s = "1010001" ;
is_binary_divisible_by_9(s);
return 0;
}
|
Java
public class BinaryDivisibleByNine {
public static String isBinaryDivisibleByNine(String s) {
int decimal_num = Integer.parseInt(s, 2 );
if (decimal_num % 9 == 0 ) {
return "Yes" ;
} else {
return "No" ;
}
}
public static void main(String[] args) {
String s = "1010001" ;
System.out.println(isBinaryDivisibleByNine(s));
}
}
|
Python3
def is_binary_divisible_by_9(s):
decimal_num = int (s, 2 )
if decimal_num % 9 = = 0 :
print ( "Yes" )
else :
print ( "No" )
s = '1010001'
is_binary_divisible_by_9(s)
|
C#
using System;
public class BinaryDivisibleByNine {
public static string IsBinaryDivisibleByNine( string s)
{
int decimal_num = Convert.ToInt32(s, 2);
if (decimal_num % 9 == 0) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void Main( string [] args)
{
string s = "1010001" ;
Console.WriteLine(IsBinaryDivisibleByNine(s));
}
}
|
Javascript
function isBinaryDivisibleByNine(s) {
let decimal_num = parseInt(s, 2);
if (decimal_num % 9 === 0) {
return "Yes" ;
} else {
return "No" ;
}
}
let s = "1010001" ;
console.log(isBinaryDivisibleByNine(s));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: If the length of a binary string is greater than 64 then the decimal representation of the binary string will cause an overflow. Therefore, to reduce the overflow issue the idea is to convert the binary string into the octal representation and check if the octal representation of the binary string is divisible by 9 or not. Follow the steps below to solve the problem:
- Convert the binary string into octal representation.
- Initialize a variable, say Oct_9 to store the octal representation of 9.
- Find the sum of digits, say evenSum present at even positions in the octal representation of the binary string.
- Find the sum of digits, say oddSum present at odd positions in the octal representation of the binary string.
- Check if abs(oddSum – EvenSum) % Oct_9 == 0 or not. If found to be true, then print Yes.
- Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string ConvertequivalentBase8(string S)
{
map<string, char > mp;
mp[ "000" ] = '0' ;
mp[ "001" ] = '1' ;
mp[ "010" ] = '2' ;
mp[ "011" ] = '3' ;
mp[ "100" ] = '4' ;
mp[ "101" ] = '5' ;
mp[ "110" ] = '6' ;
mp[ "111" ] = '7' ;
int N = S.length();
if (N % 3 == 2) {
S = "0" + S;
}
else if (N % 3 == 1) {
S = "00" + S;
}
N = S.length();
string oct;
for ( int i = 0; i < N; i += 3) {
string temp = S.substr(i, 3);
oct.push_back(mp[temp]);
}
return oct;
}
string binString_div_9(string S, int N)
{
string oct;
oct = ConvertequivalentBase8(S);
int oddSum = 0;
int evenSum = 0;
int M = oct.length();
for ( int i = 0; i < M; i += 2) {
oddSum += int (oct[i] - '0' );
}
for ( int i = 1; i < M; i += 2) {
evenSum += int (oct[i] - '0' );
}
int Oct_9 = 11;
if ( abs (oddSum - evenSum) % Oct_9
== 0) {
return "Yes" ;
}
return "No" ;
}
int main()
{
string S = "1010001" ;
int N = S.length();
cout << binString_div_9(S, N);
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static String ConvertequivalentBase8(String S)
{
HashMap<String,
Character> mp = new HashMap<String,
Character>();
mp.put( "000" , '0' );
mp.put( "001" , '1' );
mp.put( "010" , '2' );
mp.put( "011" , '3' );
mp.put( "100" , '4' );
mp.put( "101" , '5' );
mp.put( "110" , '6' );
mp.put( "111" , '7' );
int N = S.length();
if (N % 3 == 2 )
{
S = "0" + S;
}
else if (N % 3 == 1 )
{
S = "00" + S;
}
N = S.length();
String oct = "" ;
for ( int i = 0 ; i < N; i += 3 )
{
String temp = S.substring(i, i + 3 );
oct += mp.get(temp);
}
return oct;
}
static String binString_div_9(String S, int N)
{
String oct = "" ;
oct = ConvertequivalentBase8(S);
int oddSum = 0 ;
int evenSum = 0 ;
int M = oct.length();
for ( int i = 0 ; i < M; i += 2 )
oddSum += (oct.charAt(i) - '0' );
for ( int i = 1 ; i < M; i += 2 )
{
evenSum += (oct.charAt(i) - '0' );
}
int Oct_9 = 11 ;
if (Math.abs(oddSum - evenSum) % Oct_9 == 0 )
{
return "Yes" ;
}
return "No" ;
}
public static void main(String[] args)
{
String S = "1010001" ;
int N = S.length();
System.out.println(binString_div_9(S, N));
}
}
|
Python3
def ConvertequivalentBase8(S):
mp = {}
mp[ "000" ] = '0'
mp[ "001" ] = '1'
mp[ "010" ] = '2'
mp[ "011" ] = '3'
mp[ "100" ] = '4'
mp[ "101" ] = '5'
mp[ "110" ] = '6'
mp[ "111" ] = '7'
N = len (S)
if (N % 3 = = 2 ):
S = "0" + S
elif (N % 3 = = 1 ):
S = "00" + S
N = len (S)
octal = ""
for i in range ( 0 , N, 3 ):
temp = S[i: i + 3 ]
if temp in mp:
octal + = (mp[temp])
return octal
def binString_div_9(S, N):
octal = ConvertequivalentBase8(S)
oddSum = 0
evenSum = 0
M = len (octal)
for i in range ( 0 , M, 2 ):
oddSum + = ord (octal[i]) - ord ( '0' )
for i in range ( 1 , M, 2 ):
evenSum + = ord (octal[i]) - ord ( '0' )
Oct_9 = 11
if ( abs (oddSum - evenSum) % Oct_9 = = 0 ):
return "Yes"
return "No"
if __name__ = = "__main__" :
S = "1010001"
N = len (S)
print (binString_div_9(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String ConvertequivalentBase8(String S)
{
Dictionary<String,
char > mp = new Dictionary<String,
char >();
mp.Add( "000" , '0' );
mp.Add( "001" , '1' );
mp.Add( "010" , '2' );
mp.Add( "011" , '3' );
mp.Add( "100" , '4' );
mp.Add( "101" , '5' );
mp.Add( "110" , '6' );
mp.Add( "111" , '7' );
int N = S.Length;
if (N % 3 == 2)
{
S = "0" + S;
}
else if (N % 3 == 1)
{
S = "00" + S;
}
N = S.Length;
String oct = "" ;
for ( int i = 0; i < N; i += 3)
{
String temp = S.Substring(0, N);
if (mp.ContainsKey(temp))
oct += mp[temp];
}
return oct;
}
static String binString_div_9(String S, int N)
{
String oct = "" ;
oct = ConvertequivalentBase8(S);
int oddSum = 0;
int evenSum = 0;
int M = oct.Length;
for ( int i = 0; i < M; i += 2)
oddSum += (oct[i] - '0' );
for ( int i = 1; i < M; i += 2)
{
evenSum += (oct[i] - '0' );
}
int Oct_9 = 11;
if (Math.Abs(oddSum - evenSum) % Oct_9 == 0)
{
return "Yes" ;
}
return "No" ;
}
public static void Main(String[] args)
{
String S = "1010001" ;
int N = S.Length;
Console.WriteLine(binString_div_9(S, N));
}
}
|
Javascript
<script>
function ConvertequivalentBase8(S)
{
let mp = new Map();
mp.set( "000" , '0' );
mp.set( "001" , '1' );
mp.set( "010" , '2' );
mp.set( "011" , '3' );
mp.set( "100" , '4' );
mp.set( "101" , '5' );
mp.set( "110" , '6' );
mp.set( "111" , '7' );
let N = S.length;
if (N % 3 == 2)
{
S = "0" + S;
}
else if (N % 3 == 1)
{
S = "00" + S;
}
N = S.length;
let oct = "" ;
for (let i = 0; i < N; i += 3)
{
let temp = S.substring(i, i + 3);
oct += mp.get(temp);
}
return oct;
}
function binString_div_9(S, N)
{
let oct = "" ;
oct = ConvertequivalentBase8(S);
let oddSum = 0;
let evenSum = 0;
let M = oct.length;
for (let i = 0; i < M; i += 2)
oddSum += (oct[i] - '0' );
for (let i = 1; i < M; i += 2)
{
evenSum += (oct[i] - '0' );
}
let Oct_9 = 11;
if (Math.abs(oddSum - evenSum) % Oct_9 == 0)
{
return "Yes" ;
}
return "No" ;
}
let S = "1010001" ;
let N = S.length;
document.write(binString_div_9(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...