Minimum Operations to Make the Integer Zero - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
num1 = 11, num2 = 1
›
Output:
2
💡 Note:
We need 2 operations: First, 11 - (2³ + 1) = 11 - 9 = 2. Then, 2 - (2⁰ + 1) = 2 - 2 = 0. The target after subtracting 2*num2 is 11-2*1=9, which can be expressed as sum of 2 powers of 2 (8+1).
Example 2 — Impossible Case
$
Input:
num1 = 1, num2 = -7
›
Output:
-1
💡 Note:
Each operation adds to num1 (since we subtract negative num2), making it impossible to reach 0 from positive num1.
Example 3 — Single Operation
$
Input:
num1 = 3, num2 = 2
›
Output:
1
💡 Note:
One operation: 3 - (2⁰ + 2) = 3 - 3 = 0. We subtract (1+2) in 1 operation.
Constraints
- 1 ≤ num1 ≤ 109
- -109 ≤ num2 ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code