A positive integer is called binary palindromic if its binary representation (without leading zeros) reads the same from left to right and right to left.
You are given an integer N. In a single operation, you may perform one of the following:
N by 1.N by 1.Determine the minimum number of operations required to transform N into a binary palindromic number.
Note: The binary representation of a number should always be considered without leading zeros.
A single integer N.
Print a single integer — the minimum number of operations needed to convert N into a binary palindromic number.
1 ≤ N ≤ 10^92
1
The binary representation of 2 is 10, which is not a palindrome.
After increasing it by , we obtain , whose binary representation is , a palindrome.
1311Hence, the answer is 1.
3
0
The binary representation of 3 is 11, which is already a palindrome.
10
1
The binary representation of 10 is 1010.
Decreasing it by 1 gives 9, whose binary representation is 1001, a palindrome.
Therefore, only one operation is required.
Search outward from N by checking numbers at increasing distances until a binary palindrome is found.
To verify whether a number is binary palindromic:
Time Complexity: O(k × log N), where k is the minimum distance to the nearest binary palindromic number.
Space Complexity: O(log N)
Teradata • Pending
Fidelity investment • Pending
Fidelity investment • Pending
Commonwealth Bank of Australia - CBA • Pending