A binary palindrome
Practice
3 (41 votes)
Basic programming
Recursion
Recursion and backtracking
C++
Problem
48% Success 10188 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a number $$N$$. In one operation, you can either increase the value of $$N$$ by 1 or decrease the value of $$N$$ by 1.

Determine the minimum number of operations required (possibly zero) to convert number $$N$$ to a number $$P$$ such that binary representation of $$P$$ is a palindrome.

Note: A binary representation is said to be a palindrome if it reads the same from left-right and right-left.

Input format

  • The first line contains an integer $$T$$ denoting the number of test cases.
  • For each test case, the first line contains an integer $$N$$.

Output format

For each test case in a new line, print the minimum number of operations required. 

Constraints

\(1 \le T \le 10^5 \\ 0 \le N \le 2 \times 10^9\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
149 votes
Tags:
Depth-first searchBreadth-first searchEasy-Medium
Points:30
81 votes
Tags:
AlgorithmsDepth First SearchMediumString Manipulation