• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Check if a Number is Sparse

laptop infographic online business 6087062

Introduction

A sparse number is one whose binary representation does not contain consecutive 1s. This article will show two ways to check if a number is sparse, a trivial one and a more efficient one.

Problem Example

Consider the following number and its binary representation:

N: 10

Binary: 00001010

The above number is sparse because it does not contain consecutive 1s.

N: 11

Binary: 00001011

The above number is not sparse because it contains two consecutive 1s.

A Trivial Way to Check if a Number is Sparse

An immediate solution to check if a number is sparse is the following:

  • Apply the binary AND operator to the input number and 1
  • Apply the binary AND operator to the input number shifted to the right by one position and 1
  • If the results of the two previous operations are equal to each other and equal to 1 the number is not sparse
  • Repeat the above until we obtain 0. If the number is not found to be not sparse in some of the repetitions, then it is sparse

You can find the above-described algorithm implemented in the code below:

    private boolean isSparseTrivialWay(int num) {
        int beforeShift;

        while (num > 0) {
            beforeShift = num & 1;
            num = num >> 1;
            int afterShift = num & 1;
            if (beforeShift == afterShift && beforeShift == 1) {
                return false;
            }
            beforeShift = afterShift;
        }
 
        return true;
    }

    @Test
    public void isSparseTrivialWayTest() throws Exception {

        assertTrue(isSparseTrivialWay(10));
        assertFalse(isSparseTrivialWay(11));

    }

Performance of the Trivial Algorithm

The time complexity of the above-described algorithm is O(Logn)

An Efficient Way to Check if a Number is Sparse

A more efficient way to check if a number is sparse is to apply the binary AND operator to the input number and the result of the shifting of the input number by one position to the right. If the result is 0 then the number is sparse. This is because all the 1s of the two binary representations of the input number will never be in the same positions if the number is sparse.

    private boolean isSparseEfficientWay(int num) {
        int current = num;
        int shifted = num >> 1;
        if ((current & shifted) == 0) {
            return true;
        }
        return false;
    }

    @Test
    public void isSparseEfficientWayTest() throws Exception {

        assertTrue(isSparseEfficientWay(10));
        assertFalse(isSparseEfficientWay(11));

    }

Performance of the Trivial Algorithm

The time complexity of the above-described algorithm is O(1)

Conclusion

This article explains two different strategies to solve the check if a number is sparse. The example code is available on GitHub.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions