Closest (or Next) smaller and greater numbers with same number of set bits

4.8

Given a positive integer n, print the next smallest and the previous largest number that have the same number of 1 bits in their binary representation.

Examples :

Input : n = 5
Output : Closest Greater = 6
         Closest Smaller = 3
Note that 5, 6 and 3 have same number of 
set bits. 

Input : n = 11
Output : Closest Greater = 13
         Closest Smaller = 7

The Brute Force Approach
An easy approach is simply brute force: count the number of 1s in n, and then increment (or decrement) until we find a number with the same number of 1s.

Optimal Approaches
Let’s start with the code for getNext, and then move on to getPrev.

Bit Manipulation Approach for Get Next Number
If we think about what the next number should be, we can observe the following. Given the number 13948, the binary representation looks like:

1   1   0   1   1  0  0  1  1  1  1  1  0  0
13  12  11  10  9  8  7  6  5  4  3  2  1  0

We want to make this number bigger (but not too big). We also need to keep the same number of ones.
Observation: Given a number n and two bit locations i and j, suppose we flip bit i from a 1 to a 0, and bit j from a 0 to a 1. If i > j, then n will have decreased. If i < j, then n will have increased.

We know the following:

  • If we flip a zero to a one, we must flip a one to a zero.
  • The number (After two flips) will be bigger if and only if the zero-to-one bit was to the left of the one to zero bit.
  • We want to make the number bigger, but not unnecessarily bigger. Therefore, we need to flip the rightmost zero which has ones on the right of it.

To put this in a different way, we are flipping the rightmost non-trailing zero. That is, using the above example, the trailing zeros are in the 0th and 1st spot. The rightmost non-trailing zero is at bit 7. Let's call this position p.

p ==> Position of rightmost non-trailing 0.

Step 1: Flip rightmost non-trailing zero

1    1   0   1  1  0  1  1  1  1  1  1  0  0
13  12  11  10  9  8  7  6  5  4  3  2  1  0

With this change, we have increased the number of 1s of n. We can shrink the number by rearranging all the bits to the right of bit p such that the 0s are on the left and the 1s are on the right. As we do this, we want to replace one of the 1s with a 0.

A relatively easy way of doing this is to count how many ones are to the right of p, clear all the bits from 0 until p, and then add back in c1-1 ones. Let c1 be the number of ones to the right of p and c0 be the number of zeros to the right of p.

Let’s walk through this with an example.

c1 ==> Number of ones to the right of p
c0 ==> Number of zeros to the right of p.
p = c0 + c1

Step 2: Clear bits to the right of p. From before, c0 = 2. c1 = 5. p = 7.

1    1   0   1  1  0  1  0  0  0  0  0  0  0
13  12  11  10  9  8  7  6  5  4  3  2  1  0

To clear these bits, we need to create a mask that is a sequence of ones, followed by p zeros. We can do this as follows:

// all zeros except for a 1 at position p.
a = 1 << p; 

// all zeros, followed by p ones.
b = a - 1;                       

// all ones, followed by p zeros.
mask = ~b;                       

// clears rightmost p bits.
n = n & mask;                

Or, more concisely, we do:
n &= ~((1 << p) - 1).

Step 3: Add one c1 – 1 ones.

1   1   0   1   1  0  1  0  0  0  1  1  1  1
13  12  11  10  9  8  7  6  5  4  3  2  1  0

To insert c1 – 1 ones on the right, we do the following:

// 0s with a 1 at position c1– 1
a = 1 << (c1 - 1);    

// 0s with 1s at positions 0 through c1-1
b = a - 1;                

// inserts 1s at positions 0 through c1-1
n = n | b;                

Or, more concisely:
n | = (1 << (c1 - 1)) - 1;  

We have now arrived at the smallest number bigger than n with the same number of ones. The Implementation of code for getNext is below in C++

// C++ implementation of  getNext with
// same number of bits 1's is below
#include <bits/stdc++.h>
using namespace std;

// Main Function to find next smallest
// number bigger than n
int getNext(int n)
{
    /* Compute c0 and c1 */
    int c = n;
    int c0 = 0;
    int c1 = 0;

    while (((c & 1) == 0) && (c != 0))
    {
        c0 ++;
        c >>= 1;
    }
    while ((c & 1)==1)
    {
        c1++;
        c >>= 1;
    }

    // If there is no bigger number with the
    // same no. of 1's
    if (c0 +c1 == 31 || c0 +c1== 0)
        return -1;

    // position of rightmost non-trailing zero
    int p = c0 + c1;

    // Flip rightmost non-trailing zero
    n |= (1 << p);

    // Clear all bits to the right of p
    n &= ~((1 << p) - 1);

    // Insert (c1-1) ones on the right.
    n |= (1 << (c1 - 1)) - 1;

    return n;
}

// Driver Code
int main()
{
    int n = 5;   // input 1
    cout << getNext(n) << endl;

    n = 8;     // input 2
    cout << getNext(n);
    return 0;
}

Output:

6
16

 

Optimal Bit Manipulation Approach for Get Previous Number
To implement getPrev, we follow a very similar approach.

  • Compute c0 and c1. Note that c1 is the number of trailing ones, and c0 is the size of the block of zeros immediately to the left of the trailing ones.
  • Flip the rightmost non-trailing one to a zero. This will be at position p = c1 + c0.
  • Clear all bits to the right of bit p.
  • Insert c1 + 1 ones immediately to the right of position p.

Note that Step 2 sets bit p to a zero and Step 3 sets bits 0 through p-1 to a zero. We can merge these steps.

Let’s walk through this with an example.

c1 ==> number of trailing ones
c0 ==> size of the block of zeros immediately 
       to the left of the trailing ones.
p = c1 + c0

Step 1: Initial Number: p = 7. c1 = 2. c0 = 5.

1   0   0   1   1  1  1  0  0  0  0  0  1  1
13  12  11  10  9  8  7  6  5  4  3  2  1  0

Steps 2 & 3: Clear bits 0 through p.

1   0   0   1   1  1  0  0  0  0  0  0  0  0
13  12  11  10  9  8  7  6  5  4  3  2  1  0

We can do this as follows:

// Sequence of 1s
int a = ~0;               

// Sequence of 1s followed by p + 1 zeros.
int b = a << (p + 1);     

// Clears bits 0 through p.
n & = b;                  

Steps 4: Insert c1 + 1 ones immediately to the right of position p.

1   0   0   1   1  1  0  1  1  1  0  0  0  0
13  12  11  10  9  8  7  6  5  4  3  2  1  0

Note that since p =c1 + c0, then (c1 + 1) ones will be followed by (c0 – 1)zeros.

We can do this as follows:

// 0s with 1 at position (c1 + 1)
int a = 1 << (c1 + 1);    

// 0s followed by c1 + 1 ones       
int b = a - 1;                  

// c1 + 1 ones followed by c0 - 1 zeros.
int c = b << (c0 - 1);           
n |= c;

The code to implement this is below.

// C++ Implementation of  getPrev in
// Same number of bits 1's is below
#include <bits/stdc++.h>
using namespace std;

// Main Function to find next Bigger number
// Smaller than n
int getPrev(int n)
{
    /* Compute c0 and c1  and store N*/
    int temp = n;
    int c0 = 0;
    int c1= 0;

    while ((temp & 1) == 1)
    {
        c1++;
        temp = temp >> 1;
    }

    if (temp == 0)
        return -1;

    while (((temp & 1) == 0) && (temp!= 0))
    {
        c0++;
        temp  = temp >> 1;
    }

    // position of rightmost non-trailing one.
    int p = c0 + c1;

    // clears from bit p onwards
    n = n & ((~0) << (p + 1));

    // Sequence of (c1+1) ones
    int mask = (1 << (c1 + 1)) - 1;

    n = n | mask << (c0 - 1);

    return n;
}

// Driver Code
int main()
{
    int n = 6;   // input 1
    cout << getPrev(n);

    n = 16;     // input 2
    cout << endl;
    cout << getPrev(n);

    return 0;
}

Output:

5
8

 

Arithmetic Approach to Get Next Number

If c0 is the number of trailing zeros, c1 is the size of the one block immediately following, and p = c0 + c1, we can form our solution from earlier as follows:

  1. Set the p-th bit to 1.
  2. Set all bits following p to 0.
  3. Set bits 0 through c1 – 2 to 1. This will be c1 – 1 total bits.

A quick way to perform steps 1 and 2 is to set the trailing zeros to 1 (giving us p trailing ones), and then add 1. Adding one will flip all trailing ones, so we wind up with a 1 at bit p followed by p zeros. We can perform this arithmetically.

// Sets trailing 0s to 1, giving us p trailing 1s.
n += 2c0 – 1 ;

// Flips first p ls to 0s, and puts a 1 at bit p.
n += 1;

Now, to perform Step 3 arithmetically, we just do:

// Sets trailing c1 – 1 zeros to ones.
n += 2c1 – 1 – 1;

This math reduces to:
next = n + (2c0 – 1) + 1 + (2c1 – 1 – 1)
        = n + 2c0 + 2c1 – 1 – 1

The best part is that, using a little bit manipulation, it’s simple to code.

Below Implementation in C++

// C++ Implementation of getNext with
// Same number of bits 1's is below
#include <bits/stdc++.h>
using namespace std;

// Main Function to find next smallest number
// bigger than n
int getNext(int n)
{
    /* Compute c0 and c1 */
    int c = n;
    int c0 = 0;
    int c1 = 0;

    while (((c & 1) == 0) && (c != 0))
    {
        c0 ++;
        c >>= 1;
    }
    while ((c & 1)==1)
    {
        c1++;
        c >>= 1;
    }

    // If there is no bigger number with the
    // same no. of 1's
    if (c0 +c1 == 31 || c0 +c1== 0)
        return -1;

    return n + (1 << c0) + (1 << (c1 - 1)) - 1;
}

// Driver Code
int main()
{
    int n = 5; // input 1
    cout << getNext(n);

    n = 8;     // input 2
    cout << endl;
    cout << getNext(n);
    return 0;
}

Output:

6
16

 

Arithmetic Approach to Get Previous Number
If c1 is the number of trailing ones, c0 is the size of the zero block immediately following, and p =c0 + c1, we can word the initial getPrev solution as follows:

  1. Set the pth bit to 0
  2. Set all bits following p to 1
  3. Set bits 0 through c0 – 1 to 0.

We can implement this arithmetically as follows. For clarity in the example, we assume n = 10000011. This makes c1 = 2 and c0 = 5.

// Removes trailing 1s. n is now 10000000.
n -= 2c1 – 1;    

// Flips trailing 0s. n is now 01111111.
n -= 1;    

// Flips last (c0-1) 0s. n is now 01110000.
n -= 2c0 - 1 - 1;    

This reduces mathematically to:
next = n - (2c1 - 1) - 1 - ( 2c0-1 - 1) .
     = n - 2c1 - 2c0-1 + 1;

Again, this is very easy to implement.

// C++ Implementation of Arithmetic Approach to
// getPrev with Smae number of bits 1's is below
#include <bits/stdc++.h>
using namespace std;

// Main Function to find next Bigger number
// Smaller than n
int getPrev(int n)
{
    /* Compute c0 and c1  and store N*/
    int temp = n;
    int c0 = 0;
    int c1 = 0;

    while ((temp & 1) == 1)
    {
        c1++;
        temp = temp >> 1;
    }

    if (temp == 0)
        return -1;

    while (((temp & 1) == 0) && (temp!= 0))
    {
        c0++;
        temp  = temp >> 1;
    }

    return n - (1 << c1) - (1 << (c0 - 1)) + 1;
}

// Driver Code
int main()
{
    int n = 6;   // input 1
    cout << getPrev(n);

    n = 16;     // input 2
    cout << endl;
    cout << getPrev(n);

    return 0;
}

Output:

5
8

This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



4.8 Average Difficulty : 4.8/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.