Skip to main content

Count Binary Palindromic Numbers - Video Solutions

HardMathBit Manipulation

Count Binary Palindromic Numbers | LeetCode 3677 | Weekly Contest 466

Sanyam IIT Guwahati
20:141,490 views
7 video solutions available

Count Binary Palindromic Numbers - Video Solution

Watch 7 video solutions for Count Binary Palindromic Numbers, a hard level problem involving Math, Bit Manipulation. This walkthrough by Sanyam IIT Guwahati has 1,490 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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