A binary palindrome

3

41 votes
Basic Programming, Recursion, Recursion and Backtracking, C++
Problem

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

1T1050N2×109

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first test case

  • If we decrease the value of N by 1. Then N = 5, whose binary representation is 101 which is a palindrome.
  • Hence, minimum 1 operation is required.

For second test case

  • N = 9, has binary representation 1001 which is a palindrome.
  • Hence, 0 operations is required.
Editor Image

?