Fewest Steps to Reach One
Math & Bit ManipulationAuto-graded · 27 test casesLast updated
Tests greedy reasoning over binary structure to find the minimum number of halving and increment/decrement steps needed to reduce a positive integer to 1.
Problem
You have a positive integer num. Each step, you may apply exactly one of the following rules:
- If
numis even, replace it withnum // 2. - If
numis odd, replace it with eithernum + 1ornum - 1.
Return the fewest steps needed to bring num down to exactly 1.
Rules and Tie-breaking
When num is even, halving is the only available rule, so no decision is needed.
When num is odd, you must pick between adding and subtracting. The right choice depends on the two lowest bits of num:
- If the last two bits are
01(i.e.,num % 4 == 1), subtract 1. - If the last two bits are
11(i.e.,num % 4 == 3) andnum > 3, add 1. - The value
num = 3is a special case: subtract 1 (giving2 → 1) rather than adding (giving4 → 2 → 1), because the target is already one step away via the shorter path.
Constraints
1 <= num <= 2^31 - 1numis a positive integer.
Examples
Example 1
Input: fewest_steps_to_one(8)
Output: 3
Example 2
Input: fewest_steps_to_one(7)
Output: 4
Example 3
Input: fewest_steps_to_one(1)
Output: 0
Example 4
Input: fewest_steps_to_one(3)
Output: 2
Hints
Hint 1
When num is even you have no choice at all — think about where the real decisions happen and focus your energy there.
Hint 2
Look two bits ahead instead of one: does the number end in ...01 or ...11 in binary? One pattern favors subtracting, the other favors adding — figure out which and why.
Hint 3
Check what happens at num = 3 specifically: plugging into the general rule gives the wrong answer, so handle it as an explicit special case before applying the mod-4 logic.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core observation
When num is even, the rules give no choice: divide by 2. The only meaningful decision arises when num is odd, and even then the right move is fully determined by the two lowest bits. That makes the algorithm greedy — no backtracking or memoisation required.
Why the two-bit rule works
Picture the binary representation of num. The goal is to clear all bits as cheaply as possible. Each ±1 operation costs one step, and each halving costs one step.
- Ends in
01(num % 4 == 1): subtracting 1 turns...01into...00, enabling two free halvings right away. Adding 1 would turn...01into...10, giving only one free halving. - Ends in
11(num % 4 == 3): adding 1 propagates a carry that wipes out the entire run of trailing ones in one step, then halvings consume the cleared bits. Subtracting 1 would only clear the bottom bit, leaving the rest of the run untouched.
The net effect is that every odd step is immediately followed by at least one halving, so the bit-length shrinks by at least 1 every two steps. The loop runs in O(log num) iterations.
The num = 3 special case
Applying the % 4 == 3 rule blindly to 3 would add 1, giving 4 → 2 → 1 — three total steps from 3. But subtracting gives 2 → 1 — two total steps. The general carry argument assumes there is further work to do after clearing the trailing ones; at num = 3 the target is already within one halving, so subtracting is strictly better. The condition num == 3 is checked before the mod-4 test to catch this.
Correctness summary
num = 1: the loop body never executes; returns 0. ✓num = 2: even → halve → 1; returns 1. ✓num = 3: special-cased → subtract → halve; returns 2. ✓- Even values that are powers of two need only halvings; the answer equals the bit-length minus 1. ✓
Complexity
- Time: O(log num). Each pair of steps at most halves
num, so the loop runs at most roughly2 × bit_length(num)iterations. - Space: O(1). The loop uses a single counter and modifies
numin place; no recursion, no memo table.
Full solution
def fewest_steps_to_one(num: int) -> int:
steps = 0
while num > 1:
if num % 2 == 0:
# Even: only legal move is to halve.
num //= 2
elif num == 3 or num % 4 == 1:
# Odd, ends in ...01, or the edge case 3: subtract 1.
num -= 1
else:
# Odd, ends in ...11 (and num > 3): add 1 to clear the trailing run.
num += 1
steps += 1
return steps