Saturday, September 24, 2022

Is Binary Number Multiple of 3

Problem:

Given a binary number, Find, if given binary number is a multiple of 3. The given number can be big upto 2^10000. It is recommended to finish the task using one traversal of input binary string.

Example 1:

Input: S = "011"
Output: 1
Explanation: "011" decimal equivalent
is 3, which is divisible by 3.

My Answer:

    int isDivisible(String s) {
        // code here\
        java.math.BigInteger bInt = new java.math.BigInteger(s, 2);
        
        
        if ( bInt.mod(new java.math.BigInteger("3")) == java.math.BigInteger.ZERO ) {
            return 1;
        }
        
        return 0;
    }

No comments:

Post a Comment

Search in a matrix

  Problem: Given a matrix  mat [][] of size  N  x  M , where every row and column is sorted in increasing order, and a number  X  is given. ...