Longitud de los 1 consecutivos más largos en representación binaria

Dado un número n, encuentre la longitud de los 1 consecutivos más largos en su representación binaria.
Ejemplos: 

Input : n = 14
Output : 3
The binary representation of 14 is 1110.

Input : n = 222
Output : 4
The binary representation of 222 is 11011110.

Enfoque ingenuo: una forma simple sería simplemente recorrer los bits y realizar un seguimiento de la cantidad de bits establecidos consecutivos y el máximo que ha alcanzado este valor. En este enfoque, necesitamos convertirlo a representación binaria (base-2) y luego encontrar e imprimir el resultado.

C++

// C++ program to find length of the longest
// consecutive 1s in binary representation of
// a number.
#include<bits/stdc++.h>
using namespace std;
  
int maxConsecutiveOnes(int x)
{
    // Initialize result
    int count = 0;
  
    // Count the number of iterations to
    // reach x = 0.
    while (x!=0)
    {
        // This operation reduces length
        // of every sequence of 1s by one.
        x = (x & (x << 1));
  
        count++;
    }
  
    return count;
}
  
// Driver code
int main()
{
    cout << maxConsecutiveOnes(14) << endl;
    cout << maxConsecutiveOnes(222) << endl;
    return 0;
}

Java

// Java program to find length of the longest
// consecutive 1s in binary representation of
// a number.
class MaxConsecutiveOnes
{
    private static int maxConsecutiveOnes(int x)
    {
        // Initialize result
        int count = 0;
  
        // Count the number of iterations to
        // reach x = 0.
        while (x!=0)
        {
            // This operation reduces length
            // of every sequence of 1s by one.
            x = (x & (x << 1));
  
            count++;
        }
  
        return count;
    }
  
    // Driver code
    public static void main(String strings[])
    {
        System.out.println(maxConsecutiveOnes(14));
        System.out.println(maxConsecutiveOnes(222));
    }
}

Python3

# Python program to find
# length of the longest
# consecutive 1s in
# binary representation of
# a number.
  
def maxConsecutiveOnes(x):
  
    # Initialize result
    count = 0
   
    # Count the number of iterations to
    # reach x = 0.
    while (x!=0):
      
        # This operation reduces length
        # of every sequence of 1s by one.
        x = (x & (x << 1))
   
        count=count+1
      
    return count
  
# Driver code
  
print(maxConsecutiveOnes(14))
print(maxConsecutiveOnes(222))
  
# This code is contributed
# by Anant Agarwal.

C#

// C# program to find length of the 
// longest consecutive 1s in binary
// representation of a number.
using System;
  
class GFG {
      
    // Function to find length of the 
    // longest consecutive 1s in binary
    // representation of a number 
    private static int maxConsecutiveOnes(int x)
    {
          
        // Initialize result
        int count = 0;
  
        // Count the number of iterations
        // to reach x = 0.
        while (x != 0)
        {
              
            // This operation reduces length
            // of every sequence of 1s by one.
            x = (x & (x << 1));
  
            count++;
        }
  
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        Console.WriteLine(maxConsecutiveOnes(14));
        Console.Write(maxConsecutiveOnes(222));
    }
}
  
// This code is contributed by Nitin Mittal.

PHP

<?php
// PHP program to find length 
// of the longest consecutive 
// 1s in binary representation of
// a number.
  
function maxConsecutiveOnes($x)
{
    // Initialize result
    $count = 0;
  
    // Count the number of 
    // iterations to reach x = 0.
    while ($x != 0)
    {
        // This operation reduces 
        // length of every sequence
        // of 1s by one.
        $x = ($x & ($x << 1));
  
        $count++;
    }
  
    return $count;
}
  
// Driver code
echo maxConsecutiveOnes(14), "\n";
echo maxConsecutiveOnes(222), "\n";
  
// This code is contributed by Ajit
?>

Javascript

<script>
  
// Javascript program to find length 
// of the longest consecutive 1s in 
// binary representation of a number.
function maxConsecutiveOnes(x)
{
      
    // Initialize result
    let count = 0;
  
    // Count the number of iterations to
    // reach x = 0.
    while (x != 0)
    {
          
        // This operation reduces length
        // of every sequence of 1s by one.
        x = (x & (x << 1));
  
        count++;
    }
    return count;
}
  
// Driver code
document.write(maxConsecutiveOnes(14) + "<br/>");
document.write(maxConsecutiveOnes(222));
  
// This code is contributed by code_hunt    
  
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *