This question evaluates algorithmic problem-solving and integer-manipulation skills, specifically assessing the ability to reason about operation counts and binary behavior, and it belongs to the Coding & Algorithms category.
You are given a non-negative integer n.
In one operation:
n
is even, you may replace
n
with
n / 2
.
n
is odd, you may replace
n
with either
n + 1
or
n - 1
.
Return the minimum number of operations needed to make n = 0.
n
.
0
.
0 <= n <= 10^9
(or similar large bound).
n = 0
→
0
n = 7
→
5
(one optimal path: 7→8→4→2→1→0)