Encode Number - Problem

Given a non-negative integer num, return its encoding string. The encoding follows a pattern where each number maps to a binary-like representation.

The encoding pattern can be deduced from these examples:

  • 0 → "" (empty string)
  • 1 → "0"
  • 2 → "1"
  • 3 → "00"
  • 4 → "01"
  • 5 → "10"
  • 6 → "11"

Your task is to find the pattern and implement the encoding function.

Input & Output

Example 1 — Small Number
$ Input: num = 5
Output: "10"
💡 Note: Number 5 belongs to group 2 (range 3-6), at position 1 within the group. Position 1 in 2-bit binary is "10".
Example 2 — Edge Case Zero
$ Input: num = 0
Output: ""
💡 Note: Zero maps to empty string according to the encoding pattern.
Example 3 — Group Boundary
$ Input: num = 3
Output: "00"
💡 Note: Number 3 is the first in group 2, at position 0. Position 0 in 2-bit binary is "00".

Constraints

  • 0 ≤ num ≤ 109

Visualization

Tap to expand
Encode Number: Pattern RecognitionGroup 00→""Group 11→"0", 2→"1"Group 23→"00", 4→"01", 5→"10", 6→"11"Group 37-14 (3-bit encoding)5Input number5 is in Group 2, position 1 (5-4=1)Position 1 in 2-bit binary = "10"Output: "10"
Understanding the Visualization
1
Input
Given number num = 5
2
Pattern
Identify groups: 0→"", 1-2→1bit, 3-6→2bit, 7-14→3bit
3
Output
Encoded string "10"
Key Takeaway
🎯 Key Insight: Numbers are grouped by powers of 2, with each group using binary encoding of specific bit length
Asked in
Google 25 Facebook 18 Microsoft 15
23.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen