Problem Link - Decimal to binary
Problem Statement
Write a program to input an integer N in decimal form, convert it into binary, and then print the result.
Approach
The code idea is to convert a decimal number N into binary by repeatedly dividing N by 2 and storing each remainder. For each division, the remainder (N % 2
) represents a binary digit and is stored in a list in reverse order. Once N becomes 0, the binary digits are already in reverse, so printing them from the last stored digit to the first gives the correct binary representation. This method efficiently converts decimal to binary by focusing on simple division and remainder steps.
Time Complexity
O(log N) The loop iterates logarithmically as N is divided by 2.
Space Complexity
O(log N) The vector stores up to logâ‚‚(N) binary digits.