← Unit 2 · all exercises
Saved in this browser only

Steps down to one

Starting from a positive number, count how many times you can halve it before it reaches 1. Whole-number division, so 7 goes 3, 1 — that is 2 steps.

int steps(int n)

Solution.java
Loading editor…
Results

Examples

  • steps(8) → 3
  • steps(1) → 0

There are also 4 hidden tests you can't see. Solve the problem, not the examples.