Skip to main content

Count Binary Palindromic Numbers - Solution & Explanation

HardMathBit Manipulation3 min read
Practice this problem

Problem Statement

You are given a non-negative integer n.

A non-negative integer is called binary-palindromic if its binary representation (written without leading zeros) reads the same forward and backward.

Return the number of integers k such that 0 <= k <= n and the binary representation of k is a palindrome.

Note: The number 0 is considered binary-palindromic, and its representation is "0".

 

Example 1:

Input: n = 9

Output: 6

Explanation:

The integers k in the range [0, 9] whose binary representations are palindromes are:

  • 0 → "0"
  • 1 → "1"
  • 3 → "11"
  • 5 → "101"
  • 7 → "111"
  • 9 → "1001"

All other values in [0, 9] have non-palindromic binary forms. Therefore, the count is 6.

Example 2:

Input: n = 0

Output: 1

Explanation:

Since "0" is a palindrome, the count is 1.

 

Constraints:

  • 0 <= n <= 1015

Approach Overview

Problem Overview: Given an integer limit, count how many numbers have a binary representation that reads the same forward and backward. These are binary palindromes such as 1, 3 (11), 5 (101), or 9 (1001). The challenge is counting them efficiently without scanning every number.

Approach 1: Brute Force Binary Check (O(n log n) time, O(1) space)

The straightforward solution iterates from 1 to n. Convert each number to binary and check if the bit string is a palindrome using two pointers from both ends. The palindrome check takes O(log n) time because the binary length of a number is logâ‚‚ n. This approach is easy to implement but inefficient for large limits since every number must be inspected.

This method still appears in interviews as a starting point. It shows you recognize the definition of a binary palindrome and can implement a direct verification using bit manipulation or string comparison.

Approach 2: Construct Palindromes by Length (O(log n) time, O(1) space)

Instead of checking every number, generate binary palindromes directly. A binary palindrome is defined by its first half; the second half mirrors it. For a palindrome of length L, choose the first ceil(L/2) bits and mirror them to form the rest. Iterate through possible bit lengths up to logâ‚‚ n, count how many valid prefixes exist, and construct candidates using bit operations.

For each prefix, build the mirrored number using shifts and bit masking. Stop when the constructed value exceeds n. Because the number of binary lengths is only about logâ‚‚ n, and each prefix directly produces a palindrome, the algorithm avoids scanning the entire numeric range. This turns a linear search into a logarithmic one using simple operations like shifting and reversing bits.

The key observation is symmetry: once the left half is fixed, the right half is determined. Counting possible prefixes becomes a small math problem combined with efficient bit manipulation. This is the technique typically used in optimized competitive programming solutions.

Recommended for interviews: Start with the brute force explanation because it directly models the problem definition. Then move to the palindrome construction idea. Interviewers expect the optimized approach since it demonstrates understanding of binary structure, bit operations, and how to reduce a search space using symmetry.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Binary CheckO(n log n)O(1)Small n or quick prototype; easiest approach to explain
Construct Binary Palindromes by LengthO(log n)O(1)Large limits where scanning all numbers is too slow; typical optimal interview solution

Video Solution

Count Binary Palindromic Numbers | LeetCode 3677 | Weekly Contest 466 • Sanyam IIT Guwahati • 1,490 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Count Binary Palindromic Numbers easy or hard?
The problem is typically classified as Hard because the optimal solution requires recognizing binary symmetry and constructing palindromes mathematically. A brute force solution is simple, but reaching the logarithmic approach requires deeper understanding of bit patterns.
Count Binary Palindromic Numbers Python/Java solution
Implement the optimized approach by iterating through possible palindrome lengths, generating prefix bits, and mirroring them to form complete numbers. The same logic works across Python, Java, C++, and Go since all provide bit shift and masking operations needed for palindrome construction.
How to solve Count Binary Palindromic Numbers in O(log n)?
Iterate over possible binary lengths up to log2(n). For each length, generate prefixes representing the left half of the palindrome. Mirror the prefix to produce the full binary palindrome using shifts and bit masking. Count only those values less than or equal to n.
What is the best approach for Count Binary Palindromic Numbers?
The most efficient approach constructs binary palindromes directly instead of checking every number. A palindrome is determined by its first half, so you generate prefixes and mirror them using bit operations. This reduces the search space to possible bit lengths up to log2(n). The resulting complexity is about O(log n) time and O(1) space.
Is Count Binary Palindromic Numbers asked at Google/Amazon/Meta?
Binary palindrome counting appears in variations during interviews that test bit manipulation and mathematical pattern recognition. Large companies like Google, Amazon, and Meta often ask similar problems involving bit symmetry, binary construction, or palindrome generation.
What data structure is used in Count Binary Palindromic Numbers?
The optimized solution primarily relies on bit manipulation and simple arithmetic rather than complex data structures. Operations like bit shifts, masking, and prefix mirroring are used to construct palindromes efficiently.
What is the time complexity of Count Binary Palindromic Numbers?
The brute force solution runs in O(n log n) time because each number up to n is converted to binary and checked for palindrome symmetry. The optimized construction approach runs in O(log n) time since it iterates over binary lengths and builds palindromes directly using bit manipulation.

Ready to solve this problem?

Practice Count Binary Palindromic Numbers with our built-in code editor and test cases.

Practice on FleetCode