Medium · Strings
Number of Steps to Reduce a Number in Binary to One
Given the binary representation s of a positive integer (no leading zeros), return the number of steps needed to reduce it to 1, where each step halves the number if it is even and adds 1 to it if it is odd.
Examples
Example 1
{
"s": "1101"
}Output: 6 steps
Example 2
{
"s": "10"
}Output: 1 step
Rebuild it in the studio
Core interview problems are free with a full runner and lessons; Pro opens the complete library.