#3766

Minimum Operations to Make Binary Palindrome

medium · 51.7% accepted · 51 likes · top 41%

array · two pointers · binary search · bit manipulation

Description

You are given an integer array nums.

For each element nums[i], you may perform the following operations any number of times (including zero):

- Increase nums[i] by 1, or

- Decrease nums[i] by 1.

A number is called a binary palindrome if its binary representation without leading zeros reads the same forward and backward.

Your task is to return an integer array ans, where ans[i] represents the minimum number of operations required to convert nums[i] into a binary palindrome.

Solution