Maximizing Longest Sequence of Ones

AlgorithmAlgorithmBeginner
Practice Now

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

Introduction

In computer science, bit flipping is a technique used to manipulate individual bits of data. In this challenge, we will be flipping one bit from 0 to 1 to maximize the longest sequence of 1s.


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/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/bit_manipulation("`Bit Manipulation`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/break_continue("`Break and Continue`") python/DataStructuresGroup -.-> python/lists("`Lists`") 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/catching_exceptions("`Catching Exceptions`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} algorithm/bit_manipulation -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/conditional_statements -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/for_loops -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/break_continue -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/lists -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/tuples -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/function_definition -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/classes_objects -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/encapsulation -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/catching_exceptions -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/raising_exceptions -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} python/build_in_functions -.-> lab-268814{{"`Maximizing Longest Sequence of Ones`"}} end

Flip Bit

Problem

Given a binary number, we need to flip one of its bits from 0 to 1 to maximize the longest sequence of 1s. For example, if we have the binary number 000011110000, we can flip the fourth bit from 0 to 1 to get 000111110000, which has a sequence of five 1s. Our goal is to write a Python function that takes in a binary number and returns the length of the longest sequence of 1s after flipping one bit.

Requirements

The requirements for our Python function are as follows:

  • The input must be an integer in base 2.
  • We can assume the input is a 32-bit number.
  • We do not have to validate the length of the input.
  • The output must be an integer.
  • We cannot assume the inputs are valid.
  • We can assume we are using a positive number since Python doesn't have an >>> operator.
  • We can assume this fits memory.

Example Usage

Here are some examples of how our Python function should behave:

  • None -> Exception
  • 11111111111111111111111111111111 -> 32
  • 00000000000000000000000000000000 -> 1
  • 00001111110111011111001111110000 -> 10

Summary

In this challenge, we learned how to flip one bit from 0 to 1 to maximize the longest sequence of 1s in a binary number. We also wrote a Python function that takes in a binary number and returns the length of the longest sequence of 1s after flipping one bit.

Other Algorithm Tutorials you may like