Problem · Math

Minimum Operations to Make Binary Palindromic

Learn this problem
EasyGoogleINTERNOA
See Google hiring insights

Problem statement

You are given a number N (0<=N<=2*1e9) and you can do the following operation any number of times including zero. In one operation you can increase or decrease the number by 1. Your task is to find the minimum number of operations to make the binary form of the N palindromic.

Function

minOperationsToMakeBinaryPalindromic(N: int) → int

Examples

Example 1

N = 6return = 1

You need to decrease N by 1 to make it 5 as binary representation of 5 is Palindromic (101) or you can increase N by 1 to make it 7 as its binary is also palindromic (111).

Constraints

0 <= N <= 2 * 109

More Google problems

drafts saved locally
public int minOperationsToMakeBinaryPalindromic(int N) {
  // write your code here
}
N6
expected1
checking account