Skip to main content

Encode Number - Solution & Explanation

MediumPremiumFree on FleetCodeMathStringBit Manipulation4 min readAsked at: Quora
Practice this problem

Problem Statement

Given a non-negative integer num, Return its encoding string.

The encoding is done by converting the integer to a string using a secret function that you should deduce from the following table:

 

Example 1:


Input: num = 23

Output: "1000"

Example 2:


Input: num = 107

Output: "101100"

 

Constraints:

  • 0 <= num <= 10^9

Approach Overview

Problem Overview: Given a non‑negative integer num, return its encoded binary representation using a special rule. The encoding corresponds to the binary representation of num + 1 with the leading 1 removed. The result is returned as a string.

Approach 1: Binary Enumeration (Brute Force) (Time: O(n log n), Space: O(log n))

One way to understand the encoding rule is to generate binary strings sequentially and map them to integers. Start from an empty string, then enumerate binary values like "", "0", "1", "00", "01", and so on. Each step corresponds to increasing numbers. If you iterate through integers and simulate this mapping until reaching num, you can return the corresponding encoded value. This works but is inefficient because you repeatedly construct binary strings and maintain the sequence ordering. The approach mainly helps reveal the pattern behind the encoding rather than serving as a practical implementation.

Approach 2: Bit Manipulation Trick (Optimal) (Time: O(log n), Space: O(log n))

The encoding rule becomes simple once you observe the pattern: every encoded string corresponds to the binary form of num + 1 with the most significant bit removed. Compute num + 1, convert it to binary, then drop the first character. For example, if num = 23, then num + 1 = 24, which is 11000 in binary. Removing the leading 1 produces 1000, the encoded result. This works because the encoding sequence effectively enumerates binary numbers without their leading bit. The algorithm uses simple bit manipulation or binary conversion from math properties, and the result is returned as a string.

The implementation is straightforward: increment the number, convert it to binary, and slice the string starting from index 1. The runtime depends on the number of bits in num, which is O(log n). Space complexity is also O(log n) due to the binary string.

Recommended for interviews: Interviewers expect the bit manipulation observation. Brute force enumeration demonstrates reasoning about the encoding sequence, but recognizing that the sequence equals binary(num + 1) without the leading bit shows strong pattern recognition and comfort with binary representations.

Solution

We add one to num, then convert it to a binary string and remove the highest bit 1.

The time complexity is O(log n), and the space complexity is O(log n). Where n is the size of num.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Binary Enumeration (Brute Force)O(n log n)O(log n)Useful for understanding how the encoding sequence is formed
Bit Manipulation TrickO(log n)O(log n)Best approach for production and interviews; uses binary representation of n+1

Video Solution

1256. Encode NumberShuo Yan90 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Encode Number easy or hard?
Encode Number is classified as a medium problem because the implementation is simple once you discover the pattern. The challenge lies in recognizing that the encoded sequence matches binary(num + 1) with the leading bit removed.
Encode Number Python/Java solution
In Python or Java, compute num + 1, convert it to binary, and remove the first character. Python can use bin(num + 1)[3:], while Java can use Integer.toBinaryString(num + 1).substring(1). Both implementations run in O(log n) time.
How to solve Encode Number in O(log n)?
Add 1 to the given number, convert the result to binary, and remove the first bit. For example, if num = 23, then num + 1 = 24 and its binary representation is 11000. Removing the leading '1' gives 1000, which is the encoded value.
What is the best approach for Encode Number?
The optimal approach uses a bit manipulation observation: the encoded value equals the binary representation of num + 1 with the leading '1' removed. This avoids simulation and directly computes the result in O(log n) time. The method relies on properties of binary numbering.
Is Encode Number asked at Google/Amazon/Meta?
Encode Number represents the type of bit manipulation and binary pattern recognition problems commonly used in interviews at companies like Google, Amazon, and Meta. These questions test whether candidates can detect hidden relationships in binary representations.
What data structure is used in Encode Number?
The problem mainly relies on bit manipulation and string handling rather than complex data structures. The integer is converted to a binary string, and simple string slicing removes the leading bit to produce the encoded result.
What is the time complexity of Encode Number?
The optimal solution runs in O(log n) time because the algorithm converts num + 1 into its binary representation, which requires processing the number of bits in n. Space complexity is also O(log n) due to the output binary string.

Ready to solve this problem?

Practice Encode Number with our built-in code editor and test cases.

Practice on FleetCode