Skip to content
Decoder

Broken Calculator

Medium

Problem

A broken calculator shows start_value and has only two buttons: double the number, or subtract 1. Return the minimum number of presses to show target.

Example 1

Input
start_value = 2, target = 3
Output
2
Why
double, then subtract: 2 → 4 → 3

Example 2

Input
start_value = 5, target = 8
Output
2
Why
subtract, then double: 5 → 4 → 8

Example 3

Input
start_value = 3, target = 10
Output
3
Why
3 → 6 → 5 → 10

Constraints

  • 1 <= start_value, target <= 10^9

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.