# Find Next Sparse Number

A Sparse Number is form of binary number which is not have adjacent bits are 1s.

Here given code implementation process.

``````//C Program
//Find Next Sparse Number
#include <stdio.h>

int is_sparse(int n)
{
int auxiliary = -1;

for (int bits = 31 ; bits >= 0; bits--)
{
if( (n >> bits) & 1)
{
if(auxiliary==-1 || (auxiliary > 0 && auxiliary-1!=bits))
{
auxiliary=bits;
}
else
{
return 0;
}
}
}
return 1;
}

void next_sparse(int number)
{
//Assume next sparse number is under the valid 4 byte integer

int auxiliary=number;

while(1)
{

if(is_sparse(auxiliary))
{
printf("%d\n",auxiliary );
break;
}
auxiliary++;
}
}

int main()
{

next_sparse(5);
next_sparse(21);
next_sparse(53);
next_sparse(35);

return 0;
}``````

#### Output

``````5
21
64
36
``````
``````/*
C++ Program
Find Next Sparse Number
*/
#include <iostream>

using namespace std;

class Number {
public:
bool is_sparse(int n) {
int auxiliary = -1;
int result;
for (int bits = 31; bits >= 0; bits--) {
result = (n >> bits) & 1;
if (result == 1) {
if (auxiliary == -1 || (auxiliary > 0 && auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return false;
}
}
}
return true;
}
void next_sparse(int number) {
int auxiliary = number;
while (true) {
if (this->is_sparse(auxiliary)) {
cout << auxiliary << "\n";
break;
}
auxiliary++;
}
}

};

int main() {
Number obj;
obj.next_sparse(5);
obj.next_sparse(21);
obj.next_sparse(53);
obj.next_sparse(35);
return 0;
}``````

#### Output

``````5
21
64
36``````
``````/*
Java Program
find Next Sparse Number
*/
public class Number {
public boolean is_sparse(int n) {
int auxiliary = -1;
int result;
for (int bits = 31; bits >= 0; bits--) {
result = (n >> bits) & 1;

if (result == 1) {
if (auxiliary == -1 || (auxiliary > 0 && auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return false;
}
}
}
return true;
}

public void next_sparse(int number) {
//Assume next sparse number is under the valid 4 byte integer

int auxiliary = number;

while (true) {

if (is_sparse(auxiliary)) {
System.out.print(auxiliary + "\n");
break;
}
auxiliary++;
}
}
public static void main(String[] args) {
Number obj = new Number();
obj.next_sparse(5);
obj.next_sparse(21);
obj.next_sparse(53);
obj.next_sparse(35);

}
}``````

#### Output

``````5
21
64
36
``````
``````#Python3
#Find Next Sparse Number
class Number :
def is_sparse(self, n) :
auxiliary = -1
bits = 31
while ( bits >= 0 ) :
result = (n >> bits) & 1
if (result == 1) :
if (auxiliary == -1 or(auxiliary > 0 and auxiliary - 1 != bits)) :
auxiliary = bits
else :
return False
bits -= 1

return True

def next_sparse(self, number) :
auxiliary = number
while (True) :
if (self.is_sparse(auxiliary)) :
print(auxiliary )
break

auxiliary += 1

def main() :
obj = Number()
obj.next_sparse(5)
obj.next_sparse(21)
obj.next_sparse(53)
obj.next_sparse(35)

if __name__ == "__main__":
main()``````

#### Output

``````5
21
64
36
``````
``````/*
C# Program
find Next Sparse Number
*/
using System;
public class Number
{
public Boolean is_sparse(int n)
{
int auxiliary = -1;
int result;
for (int bits = 31 ; bits >= 0; bits--)
{
result = (n >> bits) & 1;

if( result ==1)
{
if(auxiliary==-1 || (auxiliary > 0 && auxiliary-1!=bits))
{
auxiliary=bits;
}
else
{
return false;
}
}
}
return true;
}

public void next_sparse(int number)
{
//Assume next sparse number is under the valid 4 byte integer

int auxiliary=number;

while(true)
{

if(is_sparse(auxiliary))
{
Console.Write(auxiliary +"\n" );
break;
}
auxiliary++;
}
}
public static void Main(String[] args) {
Number obj = new Number();
obj.next_sparse(5);
obj.next_sparse(21);
obj.next_sparse(53);
obj.next_sparse(35);

}
}``````

#### Output

``````5
21
64
36
``````
``````<?php
/*
Php Program
find Next Sparse Number
*/
class Number {
public function is_sparse(\$n) {
\$auxiliary = -1;
for (\$bits = 31; \$bits >= 0; \$bits--) {
\$result = (\$n >> \$bits) & 1;
if (\$result == 1) {
if (\$auxiliary == -1 || (\$auxiliary > 0 && \$auxiliary - 1 != \$bits)) {
\$auxiliary = \$bits;
} else {
return false;
}
}
}
return true;
}
public function next_sparse(\$number) {
\$auxiliary = \$number;
while (true) {
if (\$this->is_sparse(\$auxiliary)) {
echo  \$auxiliary ."\n";
break;
}
\$auxiliary++;
}
}
}

function main() {
\$obj = new Number();
\$obj->next_sparse(5);
\$obj->next_sparse(21);
\$obj->next_sparse(53);
\$obj->next_sparse(35);
}
main();``````

#### Output

``````5
21
64
36
``````
``````/*
Node Js Program
find Next Sparse Number
*/
class Number {
is_sparse(n) {
var auxiliary = -1;
var result;
for (var bits = 31; bits >= 0; bits--) {
result = (n >> bits) & 1;
if (result == 1) {
if (auxiliary == -1 ||
(auxiliary > 0 &&
auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return false;
}
}
}

return true;
}
next_sparse(number) {
//Assume next sparse number is under the valid 4 byte integer
var auxiliary = number;
while (true) {
if (this.is_sparse(auxiliary)) {
process.stdout.write(auxiliary + "\n");
break;
}
auxiliary++;
}
}
}

function main(args) {
var obj = new Number();
obj.next_sparse(5);
obj.next_sparse(21);
obj.next_sparse(53);
obj.next_sparse(35);
}

main();```
```

#### Output

``````5
21
64
36``````
``````# Ruby Program
# Find Next Sparse Number

class Number
def is_sparse(n)
auxiliary = -1
result = 0
bits = 31
while (bits >= 0)
result = (n >> bits) & 1
if (result == 1)
if (auxiliary == -1 ||
(auxiliary > 0 &&
auxiliary - 1 != bits))
auxiliary = bits
else
return false
end
end
bits -= 1
end
return true
end
def next_sparse(number)
auxiliary = number
while (true)
if (self.is_sparse(auxiliary))
print(auxiliary ,"\n")
break
end
auxiliary += 1
end
end
end
def main()
obj = Number.new()
obj.next_sparse(5)
obj.next_sparse(21)
obj.next_sparse(53)
obj.next_sparse(35)
end
main()```
```

#### Output

``````5
21
64
36
``````
``````/*
Scala Program
find Next Sparse Number
*/
class Number {
def is_sparse(n: Int): Boolean = {
var auxiliary: Int = -1;
var result: Int = 0;
var bits: Int = 31;
while (bits >= 0) {
result = (n >> bits) & 1;

if (result == 1) {
if (auxiliary == -1 ||
(auxiliary > 0 &&
auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return false;
}
}
bits -= 1;
}
return true;
}
def next_sparse(number: Int): Unit = {
var auxiliary: Int = number;
var status = true;
while (status == true) {
if (is_sparse(auxiliary)) {
print(" "+auxiliary + "\n");
status=false;
}
auxiliary += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: Number = new Number();
obj.next_sparse(5);
obj.next_sparse(21);
obj.next_sparse(53);
obj.next_sparse(35);
}
}```
```

#### Output

`````` 5
21
64
36``````
``````/*
Swift Program
find Next Sparse Number
*/
class Number {
func is_sparse(_ n: Int) -> Bool {
var auxiliary: Int = -1;
var result: Int;
var bits: Int = 31;
while (bits >= 0) {
result = (n >> bits) & 1;
if (result == 1) {
if (auxiliary == -1 ||
(auxiliary > 0 &&
auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return false;
}
}
bits -= 1;
}
return true;
}
func next_sparse(_ number: Int) {
var auxiliary: Int = number;
while (true) {
if (self.is_sparse(auxiliary)) {
print(auxiliary ,"\n", terminator: "");
break;
}
auxiliary += 1;
}
}
}
func main() {
let obj: Number = Number();
obj.next_sparse(5);
obj.next_sparse(21);
obj.next_sparse(53);
obj.next_sparse(35);
}
main();```
```

#### Output

``````5
21
64
36``````
``````/*
Rust Program
find Next Sparse Number
*/
fn main() {
next_sparse(5);
next_sparse(21);
next_sparse(53);
next_sparse(35);
}
fn next_sparse(number: i32) {
//Assume next sparse number is under the valid 4 byte integer
let mut auxiliary: i32 = number;
loop  {
if is_sparse(auxiliary)==true {
print!("{}\n", auxiliary);
break;
}
auxiliary += 1;
}
}
fn is_sparse(n: i32) -> bool {
let mut auxiliary: i32 = -1;
let mut bits: i32 = 31;
while bits >= 0 {
if (n >> bits) & 1 == 1 {
if auxiliary == -1 || (auxiliary > 0 && auxiliary - 1 != bits) {
auxiliary = bits;
}
else {
return false;
}
}
bits -= 1;
}
return true;
}```
```

#### Output

``````5
21
64
36``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment 