Count the Forced Halve-or-Decrement Steps

Quick Overview

Count the Forced Halve-or-Decrement Steps. Reduce num to0. At every nonzero even value, divide by2; at every odd value, subtract1.

Count the Forced Halve-or-Decrement Steps

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Reduce num to 0. At every nonzero even value, divide by 2; at every odd value, subtract 1. Return how many forced operations are performed. ```cpp class Solution { public: int numberOfSteps(int num); }; ``` ### Examples **Example 1:** ```text Input: num = 6 Output: 4 Explanation:6->3->2->1->0 uses four steps. ``` **Example 2:** ```text Input: num = 0 Output: 0 Explanation: The target is already reached. ``` ### Constraints - num is an integer in `[0,1000000]`. ### Contract considerations Input may be 0 and is never negative. ```hint Read the boundary condition Check zero input and both parity cases under the prescribed operation rule. The output counts operations rather than intermediate values. ``` ### Boundary reminders Input may be 0 and is never negative.

Overview: Count the Forced Halve-or-Decrement Steps. Reduce num to0. At every nonzero even value, divide by2; at every odd value, subtract1.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Reduce num to 0. At every nonzero even value, divide by 2; at every odd value, subtract 1. Return how many forced operations are performed.

class Solution {
public:
    int numberOfSteps(int num);
};

Examples

Example 1:

Input: num = 6
Output: 4
Explanation:6->3->2->1->0 uses four steps.

Example 2:

Input: num = 0
Output: 0
Explanation: The target is already reached.

Constraints

  • num is an integer in [0,1000000] .

Contract considerations

Input may be 0 and is never negative.

Boundary reminders

Input may be 0 and is never negative.