Java in 2 lines


#1
    public int power(String A) {
        if (A.equals("1")) return 0;
        return new BigInteger(A).bitCount() == 1 ? 1 : 0;
    }

#2

Can you please explain this solution


#3

Any power of 2 in binary will have a leading 1 bit followed by only 0s. For example 2 in binary is 10, 4 is 100, 8 is 1000, and so on. BigInteger#bitCount returns a count of the number of 1 bits set. So this should be exactly 1 for any power of 2. Only exception is the number 1 which is also 1 in binary.


#4

thanks for nice solution ! :slight_smile: