← 返回 google 的题目列表Hackerrank Coding - Minimum Operations to Reduce an Integer to 0
类型:online_judge
Problem: Minimum Operations to Reduce an Integer to 0
Given a positive integer n, you may repeatedly perform one of the following operations:
If n is even, replace n with n / 2.
If n is odd, replace n with either n + 1 or n - 1.
Return the minimum number of operations required to reduce n to 0.
Input
One integer n.
Output
An integer: the minimum number of operations.
Constraints
1 <= n <= 10^18
Examples (Tests)
Input:
1
Output:
1
Input:
3
Output:
2
Input:
7
Output:
5
Input:
8
Output:
4
Input:
15
Output:
6
Example
Input
1
Output
1