RECUR13 Editorial

Problem Explanation

You are given an integer N and you have to output that number as a Binary String.


To convert a number from Decimal to Binary, we have to append the right most bit to the string and then right shift bits so we can access the next bit. We repeat this until there are no set bits in the number. We can extract the right-most bit using (N&1) or (N\%2). And we can right-shift N using the bitwise operator (N>>=1) or by dividing N by 2. To do this using recursion we assume that the function returns the Binary String of the number so we return the string after concatenating the Binary string returned by the function after passing the number right shifted by 1 and the right-most bit as a character. As for the base condition, when there are no set bits in the number, we return an empty string.


def Binary(n):
        return ''
    return Binary(n//2) + str(n&1)

n = int(input())