Minimum Operations to Make Binary Palindrome

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.
  • 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.

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

Example 1
Inputnums = [1,2,4]
Output[0,1,1]
The values 1, 2, and 4 require 0, 1, and 1 operations respectively to become nearest binary palindromes, so ans = [0, 1, 1].
Example 2
Inputnums = [6,7,12]
Output[1,0,3]
The values 6, 7, and 12 require 1, 0, and 3 operations respectively to become nearest binary palindromes, so ans = [1, 0, 3].

Constraints

  • 1 <= nums.length <= 5000
  • 1 <= nums[i] <= 5000

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate