text: Check if decimal representation of Binary String is divisible by 9 or not | Java program to implement the above approach ; Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary Strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary String ; Traverse the binary String ; Stores 3 consecutive characters of the binary String ; Append octal representation of temp ; Function to check if binary String is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the String oct ; Update oddSum ; Traverse the String oct ; Update evenSum ; Stores octal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code
code: 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 ) ) ; } }