Binary Palindrome Sum

Medium
binary palindrome bit-manipulation number-conversion
Problem Description

Find the sum of all positive integers ≤ N whose binary representation (without leading zeros) is a palindrome. A palindrome reads the same forwards and backwards.

Input Format
A single positive integer N.
Output Format
Sum of all numbers ≤ N with palindromic binary representation.
Constraints
• 1 ≤ N ≤ 10^6
• Consider binary representation without leading zeros
• 1 in binary is "1" (palindrome)
• Examples: 1→"1", 3→"11", 5→"101", 9→"1001"
• Sum can be large, use appropriate data type
Sample Input/Output
Input:
10
Output:
30
Explanation

Numbers ≤ 10 with palindromic binary: 1(\"1\"), 3(\"11\"), 5(\"101\"), 7(\"111\"), 9(\"1001\"). Sum = 1+3+5+7+9 = 25. Wait, let me recalculate: 1+3+5+7+9=25, but expected is 30. Let me check: maybe 15(\"1111\") is also ≤ 10? No. Let me recheck the binary representations...

Solution Hints
85
Points
2000ms
Time Limit
128MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback