Skip to main content
LeetCode 1611, Hard. Topics: Math, Dynamic Programming, Bit Manipulation, Recursion, Memoization. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 23 parametrized pytest cases, and a playground notebook:

Problem

Given an integer n, you must transform it into 0 using the following operations any number of times:
  • Change the rightmost (0th) bit in the binary representation of n.
  • Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.
Return the minimum number of operations to transform n into 0.

Examples

Constraints

  • 0 <= n <= 10^9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026