Given a binary string and a number k, the task is to check whether the binary string is evenly divisible by 2k or not.
Examples :
Input : 11000 k = 2
Output : Yes
Explanation :
(11000)2 = (24)10
24 is evenly divisible by 2k i.e. 4.
Input : 10101 k = 3
Output : No
Explanation :
(10101)2 = (21)10
21 is not evenly divisible by 2k i.e. 8.
Naive Approach: Compute the binary string into decimal and then simply check whether it is divisible by 2k. But, this approach is not feasible for the long binary strings as time complexity will be high for computing decimal number from binary string and then dividing it by 2k.
Step-by-step approach:
- Convert the binary string to decimal by iterating over each digit from left to right.
- Calculate the value of 2^k using bitwise left shift operation (1 << k).
- Check if the decimal value obtained in step 1 is divisible by the value obtained in step 2 using the modulus operator.
- If the modulus is zero, return true, otherwise return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( char str[], int k)
{
int decimal_num = 0;
int base = 1;
int n = strlen (str);
for ( int i = n - 1; i >= 0; i--) {
if (str[i] == '1' ) {
decimal_num += base;
}
base *= 2;
}
return (decimal_num % (1 << k)) == 0;
}
int main()
{
char str1[] = "10101100" ;
int k = 2;
if (isDivisible(str1, k))
cout << "Yes" << endl;
else
cout << "No" << "\n" ;
char str2[] = "111010100" ;
k = 2;
if (isDivisible(str2, k))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static boolean isDivisible(String str, int k) {
int decimal_num = 0 ;
int base = 1 ;
int n = str.length();
for ( int i = n - 1 ; i >= 0 ; i--) {
if (str.charAt(i) == '1' ) {
decimal_num += base;
}
base *= 2 ;
}
return (decimal_num % ( 1 << k)) == 0 ;
}
public static void main(String[] args) {
String str1 = "10101100" ;
int k = 2 ;
if (isDivisible(str1, k))
System.out.println( "Yes" );
else
System.out.println( "No" );
String str2 = "111010100" ;
k = 2 ;
if (isDivisible(str2, k))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def is_divisible(binary_str, k):
decimal_num = 0
base = 1
n = len (binary_str)
for i in range (n - 1 , - 1 , - 1 ):
if binary_str[i] = = '1' :
decimal_num + = base
base * = 2
return decimal_num % ( 1 << k) = = 0
if __name__ = = "__main__" :
str1 = "10101100"
k = 2
if is_divisible(str1, k):
print ( "Yes" )
else :
print ( "No" )
str2 = "111010100"
k = 2
if is_divisible(str2, k):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool IsDivisible( string binaryStr, int k)
{
int decimalNum = 0;
int baseValue = 1;
int n = binaryStr.Length;
for ( int i = n - 1; i >= 0; i--)
{
if (binaryStr[i] == '1' )
{
decimalNum += baseValue;
}
baseValue *= 2;
}
return (decimalNum % (1 << k)) == 0;
}
static void Main()
{
string str1 = "10101100" ;
int k = 2;
if (IsDivisible(str1, k))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
string str2 = "111010100" ;
k = 2;
if (IsDivisible(str2, k))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isDivisible(binaryStr, k) {
let decimalNum = 0;
let base = 1;
for (let i = binaryStr.length - 1; i >= 0; i--) {
if (binaryStr[i] === '1' ) {
decimalNum += base;
}
base *= 2;
}
return decimalNum % (1 << k) === 0;
}
const str1 = "10101100" ;
let k = 2;
if (isDivisible(str1, k)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
const str2 = "111010100" ;
k = 2;
if (isDivisible(str2, k)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(n), where n is the length of the binary string.
Auxiliary Space: O(1), as we are not using any extra space.
Efficient Approach: In the binary string, check for the last k bits. If all the last k bits are 0, then the binary number is evenly divisible by 2k else it is not evenly divisible. Time complexity using this approach is O(k).
Below is the implementation of the approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( char str[], int k)
{
int n = strlen (str);
int c = 0;
for ( int i = 0; i < k; i++)
if (str[n - i - 1] == '0' )
c++;
return (c == k);
}
int main()
{
char str1[] = "10101100" ;
int k = 2;
if (isDivisible(str1, k))
cout << "Yes" << endl;
else
cout << "No"
<< "\n" ;
char str2[] = "111010100" ;
k = 2;
if (isDivisible(str2, k))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
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" );
}
}
|
Python3
def isDivisible( str , k):
n = len ( str )
c = 0
for i in range ( 0 , k):
if ( str [n - i - 1 ] = = '0' ):
c + = 1
return (c = = k)
str1 = "10101100"
k = 2
if (isDivisible(str1, k)):
print ( "Yes" )
else :
print ( "No" )
str2 = "111010100"
k = 2
if (isDivisible(str2, k)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isDivisible(String str, int k)
{
int n = str.Length;
int c = 0;
for ( int i = 0; i < k; i++)
if (str[n - i - 1] == '0' )
c++;
return (c == k);
}
public static void Main()
{
String str1 = "10101100" ;
int k = 2;
if (isDivisible(str1, k) == true )
Console.Write( "Yes\n" );
else
Console.Write( "No" );
String str2 = "111010100" ;
k = 2;
if (isDivisible(str2, k) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isDivisible(str, k)
{
let n = str.length;
let c = 0;
for (let i = 0; i < k; i++)
if (str[n - i - 1] == '0' )
c++;
return (c == k);
}
let str1 = "10101100" ;
let k = 2;
if (isDivisible(str1, k) == true )
document.write( "Yes" + "</br>" );
else
document.write( "No" );
let str2 = "111010100" ;
k = 2;
if (isDivisible(str2, k) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function isDivisible( $str , $k )
{
$n = strlen ( $str );
$c = 0;
for ( $i = 0; $i < $k ; $i ++)
if ( $str [ $n - $i - 1] == '0' )
$c ++;
return ( $c == $k );
}
$str1 = "10101100" ;
$k = 2;
if (isDivisible( $str1 , $k ))
echo "Yes" , "\n" ;
else
echo "No" , "\n" ;
$str2 = "111010100" ;
$k = 2;
if (isDivisible( $str2 , $k ))
echo "Yes" , "\n" ;
else
echo "No" , "\n" ;
?>
|
Complexity Analysis:
- Time Complexity: O(k)
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!