Find Next Largest Smallest Binary Numbers

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This Python challenge requires you to find the next largest number and the next smallest number with the same number of 1's as the given positive integer.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/bit_manipulation("`Bit Manipulation`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") subgraph Lab Skills python/comments -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} algorithm/bit_manipulation -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/variables_data_types -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/conditional_statements -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/for_loops -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/while_loops -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/tuples -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/function_definition -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/classes_objects -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/encapsulation -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/raising_exceptions -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} python/data_collections -.-> lab-268815{{"`Find Next Largest Smallest Binary Numbers`"}} end

Get Next

Problem

Given a positive integer, you need to find the next largest number and the next smallest number with the same number of 1's as the given number. For example, if the input is 0000 0000 1101 0111, the output should be 0000 0000 1101 1011 for the next largest number and 0000 0000 1100 1111 for the next smallest number.

Requirements

To solve this challenge, you need to meet the following requirements:

  • The output should be a positive integer.
  • The inputs may not be valid, so you need to handle exceptions.
  • The solution should fit in memory.

Example Usage

Here are some examples of how to use this function:

  • None -> Exception
  • 0 -> Exception
  • negative int -> Exception
  • General case:
    * Input:         0000 0000 1101 0111
    * Next largest:  0000 0000 1101 1011
    * Next smallest: 0000 0000 1100 1111

Summary

In this Python challenge, you learned how to find the next largest number and the next smallest number with the same number of 1's as the given positive integer. You also learned about the requirements for solving this challenge and saw some examples of how to use this function.

Other Algorithm Tutorials you may like