Find Next Sparse Number in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an integer value N. our task is to create a program to find the next spares Number.

Sparse Number is a special type of number whose binary conversion does not contain any adjacent 1’s.

Example: 5(101) , 16(10000)

Problem Description − For the given number N, we need to find the smallest number greater than N which is a sparse number.

Let’s take an example to understand the problem,

Input

N = 7

Output

8

Explanation

The binary of 8 is 1000 which makes it the smallest sparse number greater than n.

Solution Approach

A simple solution to the problem is checking for all numbers greater than N. and stopping till we find our first sparse number.

For this we need to loop from N to infinity and for each number check if it is a sparse number or not. If yes, break the loop otherwise continue.

Program to illustrate the working of our solution,

Example

 Live Demo

#include<iostream>
using namespace std;
bool isSpareNumber(int N){
   int currentBit = (N&1);
   int nextBit ;
   while (N!= 0){
      nextBit = currentBit;
      currentBit = (N&1);
      N >>= 1;
      if(nextBit == currentBit && nextBit == 1 && currentBit == 1)
         return false ;
   }
   return true;
}
int findNextSparseNumber(int N) {
   while(1){
      if(isSpareNumber(N))
         return N;
      N++;
   }
   return -1;
}
int main() {
   int N = 564;
   cout<<"The number is "<<N<<endl;
   cout<<"The next Sparse Number is "<<findNextSparseNumber(N);
   return 0;
}

Output

The number is 564
The next Sparse Number is 576

Efficient approach

An efficient approach to the problem is by manipulating the bits of the number. For this we will find the binary of the number and manipulate the bits where adjacency is occured. Traversing from the least significant Bit to Most Significant Bit, when we encounter a pair of 1’s together, we will replace the both 1’s with 0’s and make the next bit 1. And do this till we reach the MSB. And then convert the number binary number back to a decimal number which is our result.

Let’s take an example here,

N = 52

Binary of the number is 110100

We will traverse from LSB and find the first pair of consecutive 1’s in the binary. It is 110100 the highlighted part. Then, we will replace both 1’s with 0’s and add a one to the next bit. This makes the number 1000000, whose binary conversion is 64.

Program to illustrate the working of our solution,

Example

 Live Demo

#include<iostream>
using namespace std;
int findNextSparseNumber(int N) {
   int spNum[16];
   int n = 0;
   while (N != 0) {
      spNum[n] = (N&1);
      n++;
      N >>= 1;
   }
   n++;
   int lastCorrectedBit = 0;
   for (int i= 0 ; i< n; i++) {
      if (spNum[i] == 1 && spNum[i-1] == 1 && spNum[i+1] != 1){
         spNum[i+1] = 1;
         for (int j=i; j>=lastCorrectedBit; j--)
            spNum[j] = 0;
            lastCorrectedBit = i+1;
      }
   }
   int sparseNumber = 0;
   for (int i =0; i<n-1; i++)
      sparseNumber += spNum[i]*(1<<i);
   return sparseNumber;
}
int main() {
   int N = 564;
   cout<<"The number is "<<N<<endl;
   cout<<"The next Sparse Number is "<<findNextSparseNumber(N);
   return 0;
}

Output

The number is 564
The next Sparse Number is 576
raja
Published on 13-Mar-2021 13:05:09
Advertisements