Check divisibility of binary string by 2 ^ k | Java implementation to check whether given binary number is evenly divisible by 2 ^ k or not ; function to check whether given binary number is evenly divisible by 2 ^ k or not ; count of number of 0 from last ; if count = k , number is evenly divisible , so returns true else false ; Driver program to test above ; first example ; Second example
class GFG { static boolean isDivisible ( String str , int k ) { int n = str . length ( ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str . charAt ( n - i - 1 ) == '0' ) c ++ ; return ( c == k ) ; } public static void main ( String args [ ] ) { String str1 = "10101100" ; int k = 2 ; if ( isDivisible ( str1 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str2 = "111010100" ; k = 2 ; if ( isDivisible ( str2 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }