"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Solution Notebook"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Problem: Given a positive integer, get the next largest number and the next smallest number with the same number of 1's as the given number.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Constraints\n",
"\n",
"* Is the output a positive int?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Test Cases\n",
"\n",
"* None -> Exception\n",
"* 0 -> Exception\n",
"* negative int -> Exception\n",
"* General case:\n",
"<pre>\n",
" * Input: 0000 0000 1101 0111\n",
" * Next largest: 0000 0000 1101 1011\n",
" * Next smallest: 0000 0000 1100 1111\n",
"</pre>"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"### get_next_largest\n",
"\n",
"* Find the right-most non trailing zero, call this index\n",
" * We'll use a mask of 1 and do a logical right shift on a copy of num to examine each bit starting from the right\n",
" * Count the number of zeroes to the right of index\n",
" * While num & 1 == 0 and num_copy != 0:\n",
" * Increment number of zeroes\n",
" * Logical shift right num_copy\n",
" * Count the number of ones to the right of index\n",
" * While num & 1 == 1 and num_copy != 0:\n",
" * Increment number of ones\n",
" * Logical shift right num_copy\n",
" * The index will be the sum of number of ones and number of zeroes\n",
" * Set the index bit\n",
" * Clear all bits to the right of index\n",
" * Set bits starting from 0\n",
" * Only set (number of ones - 1) bits because we set index to 1\n",
"\n",
"We should make a note that Python does not have a logical right shift operator built in. We can either use a positive number of implement one for a 32 bit number:\n",
"\n",
" num % 0x100000000 >> n\n",
"\n",
"### get_next_smallest\n",
"\n",
"* The algorithm for finding the next smallest number is very similar to finding the next largest number\n",
" * Instead of finding the right-most non-trailing zero, we'll find the right most non-trailing one and clear it\n",
" * Clear all bits to the right of index\n",
" * Set bits starting at 0 to the number of ones to the right of index, plus one\n",
"\n",
"Complexity:\n",
"* Time: O(b), where b is the number of bits in num\n",
"* Space: O(b), where b is the number of bits in num"