2571. Minimum Operations to Reduce an Integer to 0 LeetCode Solution
In this guide, you will get 2571. Minimum Operations to Reduce an Integer to 0 LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Reduce an Integer to problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Operations to Reduce an Integer to solution in C++
- Minimum Operations to Reduce an Integer to solution in Java
- Minimum Operations to Reduce an Integer to solution in Python
- Additional Resources
Problem Statement of Minimum Operations to Reduce an Integer to
You are given a positive integer n, you can do the following operation any number of times:
Add or subtract a power of 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
Complexity Analysis
- Time Complexity: O(\log n)
- Space Complexity: O(1)
2571. Minimum Operations to Reduce an Integer to 0 LeetCode Solution in C++
class Solution {
public:
int minOperations(int n) {
// The strategy is that when the end of n is
// 1. consecutive 1s, add 1 (2^0).
// 2. single 1, substract 1 (2^0).
// 3. 0, substract 2^k to omit the last 1. Equivalently, n >> 1.
//
// e.g.
//
// n = 0b101
// n -= 2^0 -> 0b100
// n -= 2^2 -> 0b0
// n = 0b1011
// n += 2^0 -> 0b1100
// n -= 2^2 -> 0b1000
// n -= 2^3 -> 0b0
int ans = 0;
while (n > 0)
if ((n & 3) == 3) {
++n;
++ans;
} else if (n % 2 == 1) {
--n;
++ans;
} else {
n >>= 1;
}
return ans;
}
};
/* code provided by PROGIEZ */
2571. Minimum Operations to Reduce an Integer to 0 LeetCode Solution in Java
class Solution {
public int minOperations(int n) {
// The strategy is that when the end of n is
// 1. consecutive 1s, add 1 (2^0).
// 2. single 1, substract 1 (2^0).
// 3. 0, substract 2^k to omit the last 1. Equivalently, n >> 1.
//
// e.g.
//
// n = 0b101
// n -= 2^0 -> 0b100
// n -= 2^2 -> 0b0
// n = 0b1011
// n += 2^0 -> 0b1100
// n -= 2^2 -> 0b1000
// n -= 2^3 -> 0b0
int ans = 0;
while (n > 0)
if ((n & 3) == 3) {
++n;
++ans;
} else if (n % 2 == 1) {
--n;
++ans;
} else {
n >>= 1;
}
return ans;
}
}
// code provided by PROGIEZ
2571. Minimum Operations to Reduce an Integer to 0 LeetCode Solution in Python
class Solution:
def minOperations(self, n: int) -> int:
# The strategy is that when the end of n is
# 1. consecutive 1s, add 1 (2^0).
# 2. single 1, substract 1 (2^0).
# 3. 0, substract 2^k to omit the last 1. Equivalently, n >> 1.
#
# e.g.
#
# n = 0b101
# n -= 2^0 -> 0b100
# n -= 2^2 -> 0b0
# n = 0b1011
# n += 2^0 -> 0b1100
# n -= 2^2 -> 0b1000
# n -= 2^3 -> 0b0
ans = 0
while n > 0:
if (n & 3) == 3:
n += 1
ans += 1
elif n % 2 == 1:
n -= 1
ans += 1
else:
n >>= 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.