Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:
2, or1 from the number on display.Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
Example 1:
Input: startValue = 2, target = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2:
Input: startValue = 5, target = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.
Example 3:
Input: startValue = 3, target = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Constraints:
1 <= startValue, target <= 109Problem summary: There is a broken calculator that has the integer startValue on its display initially. In one operation, you can: multiply the number on display by 2, or subtract 1 from the number on display. Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
2 3
5 8
3 10
2-keys-keyboard)minimum-operations-to-make-the-integer-zero)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #991: Broken Calculator
class Solution {
public int brokenCalc(int startValue, int target) {
int ans = 0;
while (startValue < target) {
if ((target & 1) == 1) {
target++;
} else {
target >>= 1;
}
ans += 1;
}
ans += startValue - target;
return ans;
}
}
// Accepted solution for LeetCode #991: Broken Calculator
func brokenCalc(startValue int, target int) (ans int) {
for startValue < target {
if target&1 == 1 {
target++
} else {
target >>= 1
}
ans++
}
ans += startValue - target
return
}
# Accepted solution for LeetCode #991: Broken Calculator
class Solution:
def brokenCalc(self, startValue: int, target: int) -> int:
ans = 0
while startValue < target:
if target & 1:
target += 1
else:
target >>= 1
ans += 1
ans += startValue - target
return ans
// Accepted solution for LeetCode #991: Broken Calculator
struct Solution;
impl Solution {
fn broken_calc(x: i32, mut y: i32) -> i32 {
let mut res = 0;
while y > x {
if y % 2 == 0 {
y /= 2;
} else {
y += 1;
}
res += 1;
}
res + x - y
}
}
#[test]
fn test() {
let x = 2;
let y = 3;
let res = 2;
assert_eq!(Solution::broken_calc(x, y), res);
let x = 5;
let y = 8;
let res = 2;
assert_eq!(Solution::broken_calc(x, y), res);
let x = 3;
let y = 10;
let res = 3;
assert_eq!(Solution::broken_calc(x, y), res);
let x = 1024;
let y = 1;
let res = 1023;
assert_eq!(Solution::broken_calc(x, y), res);
}
// Accepted solution for LeetCode #991: Broken Calculator
function brokenCalc(startValue: number, target: number): number {
let ans = 0;
for (; startValue < target; ++ans) {
if (target & 1) {
++target;
} else {
target >>= 1;
}
}
ans += startValue - target;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.