> For the complete documentation index, see [llms.txt](https://851958789.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://851958789.gitbook.io/notes/0397_integer_replacement/slt.md).

# Solution 1 bit + greedy

if last bit is 0, then right shift, one step down if not, count bit of n-1 and n+1, we want remove as much ones as possible

for -1, only left-most bit of 1 can be removed for +1, all left-most consecutive ones will be removed

then n + 1 == n - 1, do ++n is better, except 3

special cases:

* a special case is when n = 3, n + 1 == n - 1, use n-1 will fast
* n = `INT_MAX`, n + 1 will overflow
