# Find next sparse number

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

## Find next sparse number in java

``````/*
Java program for
Find the Next Sparse Number
*/
public class Number
{
public boolean isSparse(int n)
{
int auxiliary = -1;
for (int bits = 31; bits >= 0; bits--)
{
if (((n >> bits) & 1) == 1)
{
if (auxiliary == -1 || (auxiliary > 0 &&
auxiliary - 1 != bits))
{
auxiliary = bits;
}
else
{
return false;
}
}
}
return true;
}
public void nextSparse(int number)
{
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (isSparse(number))
{
// When num is sparse
System.out.println(number);
return;
}
// increase the value by one
number++;
}
}
public static void main(String[] args)
{
// Test
}
}``````
Output
``````5
21
64
36``````

## Find next sparse number in c++

``````// Include header file
#include <iostream>

//  Stdc++11 program for
//  Find the Next Sparse Number
class Number
{
public:
bool isSparse(int n)
{
int auxiliary = -1;
for (int bits = 31; bits >= 0; bits--)
{
if (((n >> bits) & 1) == 1)
{
if (auxiliary == -1 ||
(auxiliary > 0 && auxiliary - 1 != bits))
{
auxiliary = bits;
}
else
{
return false;
}
}
}
return true;
}
void nextSparse(int number)
{
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (isSparse(number))
{
// When num is sparse
std::cout << number << std::endl;
return;
}
// increase the value by one
number++;
}
}
};
int main(int argc, char **argv){
// Test
return 0;
};``````
Output
``````5
21
64
36``````

## Find next sparse number in c

``````// Include header file
#include <stdio.h>

//  C program for
//  Find the Next Sparse Number
int isSparse(int n) {
int auxiliary = -1;
for (int bits = 31; bits >= 0; bits--) {
if (((n >> bits) & 1) == 1) {
if (auxiliary == -1 ||
(auxiliary > 0 &&
auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return 0;
}
}
}
return 1;
}
void nextSparse(int number) {
// Assume next sparse number is under the
// valid 4 byte integer.
while (1) {
if (isSparse(number)) {
// When num is sparse
printf("%d\n",number);
return;
}
// increase the value by one
number++;
}
}
int main() {
// Test
nextSparse(5);
nextSparse(21);
nextSparse(53);
nextSparse(35);
return 0;
}``````
Output
``````5
21
64
36``````

## Find next sparse number in golang

``````package main
import "fmt"
//  go lang program for
//  Find the Next Sparse Number

func isSparse( n  int)bool {
var  auxiliary  int = -1;
for  bits  := 31; bits >= 0; bits-- {
if (((n >> bits) & 1) == 1) {
if (auxiliary == -1 ||
(auxiliary > 0 && auxiliary - 1 != bits)) {
auxiliary = bits;
} else {
return false;
}
}
}
return true;
}
func nextSparse( number  int) {
// Assume next sparse number is under the
// valid 4 byte integer.
for(true) {
if (isSparse(number)) {
// When num is sparse
fmt.Println(number);
return;
}
// increase the value by one
number++;
}
}

func main() {

// Test
nextSparse(5);
nextSparse(21);
nextSparse(53);
nextSparse(35);

}``````
Output
``````5
21
64
36``````

## Find next sparse number in vb.net

``````' Include namespace system
Imports System
'  Vb.net program for
'  Find the Next Sparse Number
public Class Numbers
Public Function  isSparse(ByVal n As Integer) As Boolean
Dim auxiliary As Integer = -1
With Nothing
Dim bits As Integer = 31
While bits >= 0
if (((n >> bits) And 1) = 1) Then
if (auxiliary = -1 OrElse
(auxiliary > 0 AndAlso auxiliary - 1 <> bits)) Then
auxiliary = bits
Else
Return  False
End IF
End If
bits -= 1
End While
End With
Return  True
End Function
Public Sub nextSparse(ByVal number As Integer)
' Assume next sparse number is under the
' valid 4 byte integer.
while (True)
if (Me.isSparse(number)) Then
' When num is sparse
Console.WriteLine(number)
Return
End If
' increase the value by one
number += 1
End While
End Sub
Public Shared Sub Main(ByVal args As String())
Dim task As Numbers = New Numbers()
' Test
End Sub
End Class``````
Output
``````5
21
64
36``````

## Find next sparse number in php

``````<?php
//  Php program for
//  Find the Next Sparse Number
class Numbers
{
function isSparse(\$n)
{
\$auxiliary = -1;
for (\$bits = 31; \$bits >= 0; \$bits--)
{
if (((\$n >> \$bits) & 1) == 1)
{
if (\$auxiliary == -1 ||
(\$auxiliary > 0 && \$auxiliary - 1 != \$bits))
{
\$auxiliary = \$bits;
}
else
{
return false;
}
}
}
return true;
}
function nextSparse(\$number)
{
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (\$this->isSparse(\$number))
{
// When num is sparse
printf("%d\n",\$number);
return;
}
// increase the value by one
\$number++;
}
}

}
// Test
Output
``````5
21
64
36
``````

## Find next sparse number in node js

``````//  Node Js program for
//  Find the Next Sparse Number
class Numbers
{
isSparse(n)
{
var auxiliary = -1;
for (var bits = 31; bits >= 0; bits--)
{
if (((n >> bits) & 1) == 1)
{
if (auxiliary == -1 ||
(auxiliary > 0 && auxiliary - 1 != bits))
{
auxiliary = bits;
}
else
{
return false;
}
}
}
return true;
}
nextSparse(number)
{
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (this.isSparse(number))
{
// When num is sparse
console.log(number);
return;
}
// increase the value by one
number++;
}
}
}
// Test
Output
``````5
21
64
36``````

## Find next sparse number in typescript

``````//  Typescript program for
//  Find the Next Sparse Number
class Numbers
{
public boolean isSparse(n:number)
{
var auxiliary = -1;
for (var bits = 31; bits >= 0; bits--)
{
if (((n >> bits) & 1) == 1)
{
if (auxiliary == -1 ||
(auxiliary > 0 && auxiliary - 1 != bits))
{
auxiliary = bits;
}
else
{
return false;
}
}
}
return true;
}
public  nextSparse(number:number)
{
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (this.isSparse(number))
{
// When num is sparse
console.log(number);
return;
}
// increase the value by one
number++;
}
}
public static  main()
{
// Test
}
}
Numbers.main();
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/``````
Output
``````5
21
64
36``````

## Find next sparse number in python

``````#  Python 3 program for
#  Find the Next Sparse Number
class Numbers :
def  isSparse(self, n) :
auxiliary = -1
bits = 31
while (bits >= 0) :
if (((n >> bits) & 1) == 1) :
if (auxiliary == -1 or
(auxiliary > 0 and
auxiliary - 1 != bits)) :
auxiliary = bits
else :
return False
bits -= 1
return True
def nextSparse(self, number) :
# Assume next sparse number is under the
# valid 4 byte integer.
while (True) :
if (self.isSparse(number)) :
# When num is sparse
print(number)
return
# increase the value by one
number += 1

if __name__=="__main__":
# Test
Output
``````5
21
64
36``````

## Find next sparse number in ruby

``````#  Ruby program for
#  Find the Next Sparse Number
class Numbers
def isSparse( n)
auxiliary = -1
bits = 31
while (bits >= 0)
if (((n >> bits) & 1) == 1)
if (auxiliary == -1 ||
(auxiliary > 0 && auxiliary - 1 != bits))
auxiliary = bits
else
return false
end
end
bits -= 1
end
return true
end
def nextSparse( number)
# Assume next sparse number is under the
# valid 4 byte integer.
while (true)
if (self.isSparse(number))
# When num is sparse
print(number,"\n")
return
end
# increase the value by one
number += 1
end
end
end

# Test
Output
``````5
21
64
36
``````

## Find next sparse number in scala

``````//  Scala program for
//  Find the Next Sparse Number
class Numbers ()
{
def isSparse(n : Int) : Boolean=
{
var auxiliary = -1
var bits = 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
}
def nextSparse(num : Int) : Unit=
{
var number = num;
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (isSparse(number))
{
// When num is sparse
println(number)
return
}
// increase the value by one
number += 1
}
}
}

object Main
{
def main(args : Array[String]) : Unit=
{
// Test
}
}``````
Output
``````5
21
64
36``````

## Find next sparse number in swift

``````import Foundation
//  Swift program for
//  Find the Next Sparse Number
class Numbers
{
func isSparse(_ n: Int) -> Bool
{
var auxiliary: Int = -1;
var bits: Int = 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;
}
func nextSparse(_ num: Int)
{
var number = num;
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (self.isSparse(number))
{
// When num is sparse
print(number);
return;
}
// increase the value by one
number += 1;
}
}
}

// Test
Output
``````5
21
64
36``````

## Find next sparse number in kotlin

``````//  Kotlin program for
//  Find the Next Sparse Number
class Numbers {
fun isSparse(n : Int) : Boolean
{
var auxiliary : Int = -1;
var bits : Int = 31;
while (bits >= 0)
{
if (((n shr bits) and 1) == 1)
{
if (auxiliary == -1 ||
(auxiliary > 0 && auxiliary - 1 != bits))
{
auxiliary = bits;
}
else
{
return false;
}
}
bits -= 1;
}
return true;
}
fun nextSparse(num : Int) : Unit
{
var number = num;
// Assume next sparse number is under the
// valid 4 byte integer.
while (true)
{
if (this.isSparse(number))
{
// When num is sparse
println(number);
return;
}
// increase the value by one
number += 1;
}
}
}
fun main(args : Array<String>) : Unit
{
val task : Numbers = Numbers();
// Test
}``````
Output
``````5
21
64
36``````

## Find next sparse number in rust

``````/*
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``````

## Comment

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.