Check if the Decimal representation of the given Binary String is divisible by K or not


A binary string is a string that consists of only two different types of characters and that is ‘0’ and ‘1’, hare base is 2. And a decimal representation means each digit is lie between ‘0’ to ‘9’, here the base is 10. Here we have given a string of binary numbers and an integer k. we have to check if the decimal representation of the given binary string is divisible by k or not. If it is divisible then we have to return ‘yes’ otherwise return ‘no’.

In the conversion of binary to decimal, we convert a base 2 number to a base 10 number by using simple methods. For example, if 1010(2) is a binary number then its equivalent decimal number is 1010(10).

Input 1: str = “1010” , k = 5
Output 1: yes

Explanation − a decimal representation of a given binary string (1010) is 10. And 10 is divisible by 5, therefore the answer is ‘yes’.

Input 2: str = “1100” , k = 5
Output 2: no

Explanation − a decimal representation of a given binary string (1100) is 12. And 12 is not divisible by 5, therefore the answer is ‘no’.

We have seen the example above for the given string, let us move to the approaches −

Approach1: For short string length

We can solve this problem using a simple conversion method of binary to decimal.

Let’s discuss this approach step by step below-

  • First, we will create a function ‘convertBinaryToDecimal’ that will take the given string ‘str’ as the parameter and will return the integer ‘decimalVal’ decimal representation of string str.

  • In the function, we traverse the string ‘str’ and calculate the decimal representation of it.

  • In the main function, create integer ‘decimalValue’ in which we store the integer return by ‘convertBinaryToDecimal’ function.

  • We create another function ‘check’ that will take the ‘decimalValue’ integer and the number ‘k’ to check whether the decimal representation is divisible by ‘k’ or not.

  • In this function

    • If ‘decimalValue’ is divisible by ‘k’ return string ‘yes’

    • Otherwise return ‘no’

NOTE: This method only works if the length of the string is up to 32.

Example

#include <bits/stdc++.h>
using namespace std;
long convertBinaryToDecimal( string str ){
   long decimalVal = 0;
   int len = str.size(); //getting length of the string str
   for(int i=0;i<len; i++){
      if(str[len-1-i] == '1'){
         decimalVal += ( int ) pow( 2 , i );
      }
   }
   return decimalVal;
}
//function to check decimalValue is divisible by k or not?
string check( long decimalValue, int k ){
   if( decimalValue % k == 0 ){
      return "Yes"; // if it is divisible
   } else {
      return "No"; // if it is not divisible
   }
}
// main function 
int main(){
   int k = 5; // given number
   string str = "1010";// given string 
   // calling the function 'convertBinaryToDecimal' to convert binary to decimal
   long decimalValue = convertBinaryToDecimal( str );
   // calling the function 'check' to check whether the 'decimalValue' is divisible by k or not
   string result = check( decimalValue, k );
   cout<<result;
   return 0;
}

Output

Yes

Time and Space Complexity

The time complexity of the above code is O(N*(logN)), as we traversed over the string and used the power function will add the factor of the log.

The space complexity of the above code is O(1), as we are not using any extra space.

Where N is the size of the string.

Approach2: For large string length

If the length of the string is too large then the optimal solution is discussed below

Here the approach is the same as the previous one the only difference is that we are taking mod with the integer k at every step while we are converting binary to decimal.

Example

#include <bits/stdc++.h>
using namespace std;
long convertBinaryToDecimal( string str, int k ){
   long decimalVal = 0;
   int len = str.size(); //getting the length of the string str
   // Creating temp variable to store power of two
   int temp = 1%k;
   if(str[len-1] == '1'){
      decimalVal += temp;
      decimalVal %= k;
   }
   // traverse string using for loop for converting binary to decimal
   for(int i=1; i<len; i++){
      //storing power of two using a previous value of temp
      temp = (temp*(2%k))%k; 
      if(str[len-1-i] == '1'){
         decimalVal += temp;
         decimalVal %= k;
      }
   }
   //return final reminder left 
   return decimalVal;
}
//function to check decimalValue is divisible by k or not?
string check( long decimalValue, int k ){
   if( decimalValue % k == 0 ){
      return "Yes"; // if it is divisible
   } else {
      return "No"; // if it is not divisible
   }
}
// main function 
int main(){
   int k = 5; // given number
   string str = "1010";// given string 
   // calling the function 'convertBinaryToDecimal' to convert binary to decimal
   long decimalValue = convertBinaryToDecimal( str, k );
   // calling the function 'check' to check whether the 'decimalValue' is divisible by k or not
   string result = check( decimalValue, k );
   cout<<result;
   return 0;
}

Output

Yes

Time and Space Complexity

The time complexity of the above code is O(N), as we traverse the spring.

The space complexity of the above code is O(1), as we are not using any extra space.

Where N is the size of the string.

Conclusion

In this tutorial, we have implemented a program to Check if the Decimal representation of the given Binary String is divisible by K or not. We have implemented two approaches for sort length string and another one for any string size. The time complexity of both approaches is O(N*(logN)) and O(N) respectively. Where N is the size of the string. The space complexity of approaches is the same as O(1).

Updated on: 16-May-2023

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements