JavaScript Program to Check if a Number is Sparse or Not
Last Updated :
09 Jun, 2024
A number is considered sparse if it does not have any consecutive ones in its binary representation.
Example:
Input: n=21
Output: True
Explanation: there are no consecutive 1s in the binary representation (10101). Therefore, 21 is a sparse number.
Input: n=22
Output: False
Explanation: there are consecutive 1s in the binary representation (10110). Therefore, 22 is bot a sparse number.
Below are the approaches to check if a number is sparse is not:
1. By Using Bitwise Operators
This approach uses bitwise operators to check if a number is sparse. It works by shifting the binary representation of the number to the left by one position and then performing a bitwise AND operation with the original number. If the result of the bitwise AND operation is greater than 0, it means that there are two consecutive '1's in the binary representation which means the number is not sparse.
Example: Implementation to check if a number is sparse or not using Bitwise Operators.
JavaScript
function isSparse(num) {
if ((num & (num >> 1)) > 0) {
return false;
}
else {
return true;
}
}
console.log(isSparse(10))
console.log(isSparse(6))
2. By converting to Binary String
In this approach we converts the number to a binary string using toString(2) and then checks if there are any occurrences of '11' in the string. If there are no consecutive '1's the number is sparse.
Example: Implementation to check if a number is sparse or not by converting the number to Binary String.
JavaScript
function isSparse(num) {
const binary = num
.toString(2);
return !/11/
.test(binary);
}
console.log(isSparse(21));
console.log(isSparse(31));
3. Using Regular Expression
In this approach we are using regular expression to check if the binary representation of the number contains consecutive 1s.
Example: Implementation to check if a number is sparse or not using regular expression.
JavaScript
function isSparse(num) {
return !/(11)/.test(num.toString(2));
}
console.log(isSparse(6));
console.log(isSparse(72));
4. By Manually Iterating Through Binary Digits
In this approach, we convert the number to its binary representation and then manually iterate through each bit to check for consecutive '1's. If we find two consecutive '1's, we return false indicating the number is not sparse. If we complete the iteration without finding consecutive '1's, we return true.
Example: Implementation to check if a number is sparse or not by manually iterating through binary digits.
JavaScript
function isSparse(num) {
const binary = num.toString(2);
for (let i = 0; i < binary.length - 1; i++) {
if (binary[i] === '1' && binary[i + 1] === '1') {
return false;
}
}
return true;
}
console.log(isSparse(21));
console.log(isSparse(22));
Similar Reads
JavaScript Program to Check Whether a Number is Harshad Number A Harshad number (also called Niven number) is a number that is divisible by the sum of its digits. In other words, if you take a number, sum up its digits, and if the original number is divisible by that sum, then it's a Harshad number. For example, 18 is a Harshad number because the sum of its dig
2 min read
JavaScript Program to Check if a Number has Bits in an Alternate Pattern JavaScript can be used to assess whether a given number follows an alternating pattern in its binary representation. By examining the binary digits of the number, one can determine if the sequence alternates between 0s and 1s, aiding in understanding the binary structure of the input. Examples: Inpu
2 min read
Javascript Program to Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem
2 min read
JavaScript Program to Check if a Number is Float or Integer In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read
PHP Program to Check the Given Matrix is Sparse or Not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem
2 min read
How to check whether a number is NaN or finite in JavaScript ? When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN
3 min read