10.20 Binary Representation
10.20.1 Problem Metadata
- Platform: Firecode.io
- Problem ID: Binary Representation
- Difficulty: Level 2
- URL: https://www.firecode.io/
- Tags:
- Techniques: Divide and Conquer, Math, String
10.20.2 Description
Given a positive integer n, return its binary representation as a string consisting of only '0' and '1'.
10.20.5 Solution - Iterative Division
10.20.5.1 Walkthrough
Repeatedly divide by two and track remainders. Appending each remainder to the front of the result (or using a StringBuilder and reversing) yields the binary digits from most significant to least significant.
10.20.5.2 Analysis
- Time Complexity: O(log n) divisions
- Space Complexity: O(log n) to store the output