You are given an integer array nums.
For each element nums[i], you may perform the following operations any number of times (including zero):
nums[i] by 1, ornums[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.
Example 1:
Input: nums = [1,2,4]
Output: [0,1,1]
Explanation:
One optimal set of operations:
nums[i] |
Binary(nums[i]) |
Nearest Palindrome |
Binary (Palindrome) |
Operations Required | ans[i] |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | Already palindrome | 0 |
| 2 | 10 | 3 | 11 | Increase by 1 | 1 |
| 4 | 100 | 3 | 11 | Decrease by 1 | 1 |
Thus, ans = [0, 1, 1].
Example 2:
Input: nums = [6,7,12]
Output: [1,0,3]
Explanation:
One optimal set of operations:
nums[i] |
Binary(nums[i]) |
Nearest Palindrome |
Binary (Palindrome) |
Operations Required | ans[i] |
|---|---|---|---|---|---|
| 6 | 110 | 5 | 101 | Decrease by 1 | 1 |
| 7 | 111 | 7 | 111 | Already palindrome | 0 |
| 12 | 1100 | 15 | 1111 | Increase by 3 | 3 |
Thus, ans = [1, 0, 3].
Constraints:
1 <= nums.length <= 50001 <= nums[i] <= 5000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this problem using a brute force strategy, we will examine every possible whole number starting from our given starting point and check them one by one. For each number, we will convert it into its binary pattern of zeros and ones and test whether it reads the exact same forwards and backwards. The very first number that satisfies this palindrome condition will be the winner, and we can then count how many steps away it is from our original starting point.
Here's how the algorithm would work step-by-step:
def min_operations_binary_palindrome(starting_number):
current_number = starting_number
while True:
binary_string = bin(current_number)[2:]
# We check equality against the reversed string to determine if the current binary pattern is symmetric
if binary_string == binary_string[::-1]:
return current_number - starting_number
current_number += 1Instead of checking every single possible binary combination, the optimal approach works by examining how the binary number reads forward and backward. It systematically adjusts the bits at symmetric positions from the outside in, ensuring we make the fewest changes needed to achieve a mirror image.
Here's how the algorithm would work step-by-step:
def min_operations_binary_palindrome(target_number):
binary_digits_list = list(bin(target_number)[2:])
left_pointer = 0
right_pointer = len(binary_digits_list) - 1
operation_counter = 0
# We work inward from both ends to align outer bits first for optimal changes.
while left_pointer < right_pointer:
if binary_digits_list[left_pointer] != binary_digits_list[right_pointer]:
binary_digits_list[right_pointer] = binary_digits_list[left_pointer]
operation_counter += 1
left_pointer += 1
right_pointer -= 1
# Reconstruct the numeric value to guarantee the final state matches expectations.
final_palindrome_value = int("".join(binary_digits_list), 2)
# Verification ensures our modifications successfully achieved the target properties.
binary_representation = bin(final_palindrome_value)[2:]
if binary_representation == binary_representation[::-1]:
return operation_counter
return operation_counter| Case | How to Handle |
|---|---|
| Empty string or single character string | Return zero operations immediately because an empty or single character string is already a valid palindrome. |
| String with even length versus odd length | The two-pointer comparison naturally stops at the middle, correctly ignoring the center character for odd lengths. |
| String already a palindrome | The comparison loop will find matching characters at each symmetric position and correctly return zero operations. |
| String with all zeros or all ones | Symmetric elements will always match, resulting in zero operations required. |
| Maximum length string scaling efficiently | A linear scan using two pointers runs in O(N) time complexity and O(1) auxiliary space, easily passing within execution limits. |
| Strings with alternating patterns like 010101 | The algorithm compares corresponding symmetric characters and accumulates the minimum flips needed for each mismatch. |