729. Convert Number to Binary Using Recursion

EasyRecursionRecursion

Given an integer n, return its binary representation as a string (with no leading zeros, except that n = 0 should return "0"). For negative numbers, prepend a minus sign to the binary string of the absolute value (e.g., -5 -> "-101"). Implement the conversion recursively.

Input: A single integer n.

Output: Return a string containing the binary representation.

Examples

Example 1
Input: 10
Output: 1010
Explanation: 10 in binary is 1010.
Example 2
Input: 0
Output: 0
Explanation: Zero is represented as '0'.
Example 3
Input: -5
Output: -101
Explanation: 5 is 101; negative sign is prepended.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →