Javatpoint Logo
Javatpoint Logo

Sparse Number in Java

In this tutorial, we will be discussing Sparse numbers in Java. A sparse number is a number whose binary representation does not contain any two or more than two continuous set bits. Let's understand it with the help of a few examples.

Example 1:

Input

int n = 12

Output: 12 is not a parse number.

Explanation: It is because the binary representation of 12 is 1100, which contains two consecutive set bits.

Example 2:

Input

int n = 10

Output: 10 is a parse number.

Explanation: It is because the binary representation of 10 is 1010, which does not contain two consecutive set bits.

Example 3:

Input

int n = 0

Output: 0 is a parse number.

Explanation: It is because the binary representation of 0 is 0, which does not contain two consecutive set bits.

Example 4:

Input

int n = 1

Output: 1 is a parse number.

Explanation: It is because the binary representation of 1 is 1, which does not contain two consecutive set bits.

Approach Using ArrayList

We can store the binary representation of the number in an array list, and then we can traverse that array list to check whether the two consecutive elements of the array list contain 1 or not. If it contains two consecutive 1's then the given number is not a sparse number; otherwise, a sparse number. Observe the following program.

FileName: SparseNumber.java

Output:

The Spare numbers lying between 1 to 20 are: 
1 2 4 5 8 9 10 16 17 18 20

Complexity Analysis: The time complexity of the above program is O(D). The space complexity of the program is O(D), as we are using an array list of the size D, where D is the total number of digits present in the binary representation of the number n.

The time, as well as the space complexity of the program, can be reduced further. The following approach shows the same.

Approach: Using Right Shift Operator

We can use the right shift operator for computing the sparse number. We know that the two consecutive bits can never be set, and we will use this property to compute the sparse number. Let's understand with the help of an example.

12 = 00001100 (in binary representation), if we do the right shift by 1 bit we get 00000110, and 00001100 & 00000110 = 00000100 which is equal to 4 (greater than zero). If we take a number whose two consecutive bits are not set, and then we do the right shift by one bit, we get zero.

10 = 00001010 (in binary representation), if we do the right shift by 1 bit we get 00000101, and 00001010 & 00000101 = 0. Thus we see that if we take a number whose consecutive bits are not set, we get the 0 when we do the bitwise & operation on the number and the number that we get after the right shift of bits by 1.

FileName: SparseNumber1.java

Output:

The Spare numbers lying between 1 to 20 are: 
1 2 4 5 8 9 10 16 17 18 20

Complexity Analysis: The time, as well as the space complexity of the program, is constant, i.e., O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA