You are given a positive integer n
, you can do the following operation any number of times:
2
from n
.Return the minimum number of operations to make n
equal to 0
.
A number x
is power of 2
if x == 2i
where i >= 0
.
Example 1:
Input: n = 39 Output: 3 Explanation: We can do the following operations: - Add 20 = 1 to n, so now n = 40. - Subtract 23 = 8 from n, so now n = 32. - Subtract 25 = 32 from n, so now n = 0. It can be shown that 3 is the minimum number of operations we need to make n equal to 0.
Example 2:
Input: n = 54 Output: 3 Explanation: We can do the following operations: - Add 21 = 2 to n, so now n = 56. - Add 23 = 8 to n, so now n = 64. - Subtract 26 = 64 from n, so now n = 0. So the minimum number of operations is 3.
Constraints:
1 <= n <= 105
When 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:
The brute force approach for this problem is like trying every possible combination of additions and subtractions. We explore every single path, step by step, until we hit zero. It's an exhaustive search, guaranteeing we'll find the fewest steps eventually.
Here's how the algorithm would work step-by-step:
def minimum_operations_brute_force(number):
minimum_operations = float('inf')
def find_nearest_power_of_two(number_input):
power = 0
while (1 << power) <= number_input:
power += 1
return 1 << (power - 1)
def solve(current_number, operations_count):
nonlocal minimum_operations
if current_number == 0:
minimum_operations = min(minimum_operations, operations_count)
return
if operations_count >= minimum_operations:
return
nearest_power_of_two = find_nearest_power_of_two(current_number)
# Explore adding the nearest power of two
solve(current_number - nearest_power_of_two, operations_count + 1)
# Explore subtracting the nearest power of two
solve(current_number + nearest_power_of_two, operations_count + 1)
# Start the recursive search
solve(number, 0)
return minimum_operations
The key insight is to recognize the number's binary representation. We want to strategically add or subtract powers of two to efficiently reach zero.
Here's how the algorithm would work step-by-step:
def minimum_operations_to_reduce_integer_to_zero(number):
operations_count = 0
while number > 0:
least_significant_bit = number & 1
if least_significant_bit == 0:
number >>= 1
operations_count += 1
else:
# Find the length of consecutive ones
consecutive_ones_length = 0
temp_number = number
while (temp_number & 1) == 1:
consecutive_ones_length += 1
temp_number >>= 1
# Check if it is more efficient to add or subtract
if consecutive_ones_length == 1 or (number >> consecutive_ones_length) & 1 == 0:
number -= (1 << (consecutive_ones_length - 1))
number >>= (consecutive_ones_length - 1)
operations_count += consecutive_ones_length
else:
#It is more efficient to add a power of two
number += (1 << consecutive_ones_length) - (1 << (consecutive_ones_length - 1))
number >>= consecutive_ones_length
operations_count += consecutive_ones_length
return operations_count
Case | How to Handle |
---|---|
n is 0 | Return 0, as no operations are needed. |
n is 1 | Return 1, as one subtraction operation is needed (1 - 2^0). |
n is a power of 2 (e.g., n = 2, 4, 8, 16) | Return 1, as one subtraction operation is needed (n - 2^k where 2^k = n). |
n is a large number (close to the maximum integer value) | The solution should scale efficiently, likely using dynamic programming or a greedy approach with logarithmic complexity, to avoid exceeding time limits. |
n is slightly larger than a power of 2 (e.g., n = 9, slightly bigger than 8 = 2^3) | The algorithm correctly considers both adding and subtracting powers of 2 in these cases to find the minimum operations. |
Integer overflow when calculating powers of 2 | Ensure powers of 2 are calculated and stored appropriately to prevent overflows, consider using a long integer type. |
Negative input values | The problem states that n is a positive integer, so throw an exception or return an appropriate error code to handle invalid input. |
Multiple possible valid solutions | The algorithm aims to find one valid solution, the one with the minimum number of operations, and returns that value. |