LeetCode solutions

1033. Broken Calculator

My accepted C++ solution to LeetCode problem 1033, Broken Calculator, running in 0ms.

  • Difficulty: Medium
  • C++
  • Runtime 0ms
  • Memory 7.9MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 7.9MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    int brokenCalc(int startValue, int target) {
        int ops = 0;
        while (target > startValue) {
            if (target % 2 == 0) {
                target /= 2;
            } else {
                target += 1;
            }
            ops++;
        }
        return ops + startValue - target;
    }
};

Source