2749. Minimum Operations to Make the Integer Zero LeetCode Solution
In this guide, you will get 2749. Minimum Operations to Make the Integer Zero LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make the Integer Zero 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 Make the Integer Zero solution in C++
- Minimum Operations to Make the Integer Zero solution in Java
- Minimum Operations to Make the Integer Zero solution in Python
- Additional Resources
Problem Statement of Minimum Operations to Make the Integer Zero
You are given two integers num1 and num2.
In one operation, you can choose integer i in the range [0, 60] and subtract 2i + num2 from num1.
Return the integer denoting the minimum number of operations needed to make num1 equal to 0.
If it is impossible to make num1 equal to 0, return -1.
Example 1:
Input: num1 = 3, num2 = -2
Output: 3
Explanation: We can make 3 equal to 0 with the following operations:
– We choose i = 2 and subtract 22 + (-2) from 3, 3 – (4 + (-2)) = 1.
– We choose i = 2 and subtract 22 + (-2) from 1, 1 – (4 + (-2)) = -1.
– We choose i = 0 and subtract 20 + (-2) from -1, (-1) – (1 + (-2)) = 0.
It can be proven, that 3 is the minimum number of operations that we need to perform.
Example 2:
Input: num1 = 5, num2 = 7
Output: -1
Explanation: It can be proven, that it is impossible to make 5 equal to 0 with the given operation.
Constraints:
1 <= num1 <= 109
-109 <= num2 <= 109
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2749. Minimum Operations to Make the Integer Zero LeetCode Solution in C++
class Solution {
public:
int makeTheIntegerZero(int num1, int num2) {
// If k operations are used, num1 - [(num2 + 2^{i_1}) + (num2 + 2^{i_2}) +
// ... + (num2 + 2^{i_k})] = 0. So, num1 - k * num2 = (2^{i_1} + 2^{i_2} +
// ... + 2^{i_k}), where i_1, i_2, ..., i_k are in the range [0, 60].
// Note that for any number x, we can use "x's bit count" operations to make
// x equal to 0. Additionally, we can also use x operations to deduct x by
// 2^0 (x times), which also results in 0.
for (long ops = 0; ops <= 60; ++ops) {
const long target = num1 - ops * num2;
if (__builtin_popcountl(target) <= ops && ops <= target)
return ops;
}
return -1;
}
};
/* code provided by PROGIEZ */
2749. Minimum Operations to Make the Integer Zero LeetCode Solution in Java
class Solution {
public int makeTheIntegerZero(int num1, int num2) {
// If k operations are used, num1 - [(num2 + 2^{i_1}) + (num2 + 2^{i_2}) +
// ... + (num2 + 2^{i_k})] = 0. So, num1 - k * num2 = (2^{i_1} + 2^{i_2} +
// ... + 2^{i_k}), where i_1, i_2, ..., i_k are in the range [0, 60].
// Note that for any number x, we can use "x's bit count" operations to make
// x equal to 0. Additionally, we can also use x operations to deduct x by
// 2^0 (x times), which also results in 0.
for (long ops = 0; ops <= 60; ++ops) {
long target = num1 - ops * num2;
if (Long.bitCount(target) <= ops && ops <= target)
return (int) ops;
}
return -1;
}
}
// code provided by PROGIEZ
2749. Minimum Operations to Make the Integer Zero LeetCode Solution in Python
class Solution:
def makeTheIntegerZero(self, num1: int, num2: int) -> int:
# If k operations are used, num1 - [(num2 + 2^{i_1}) + (num2 + 2^{i_2}) +
# ... + (num2 + 2^{i_k})] = 0. So, num1 - k * num2 = (2^{i_1} + 2^{i_2} +
# ... + 2^{i_k}), where i_1, i_2, ..., i_k are in the range [0, 60].
# Note that for any number x, we can use "x's bit count" operations to make
# x equal to 0. Additionally, we can also use x operations to deduct x by
# 2^0 (x times), which also results in 0.
for ops in range(61):
target = num1 - ops * num2
if target.bit_count() <= ops <= target:
return ops
return -1
# 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.