Mayor potencia de 2 que divide un número representado en binario

Dada la string binaria str , la tarea es encontrar la mayor potencia de 2 que divide el equivalente decimal del número binario dado.

Ejemplos:  

Entrada: str = “100100” 
Salida:
2 2 = 4 es la potencia más alta de 2 que divide a 36 (100100).
Entrada: str = “10010” 
Salida: 1  

Enfoque: comenzando desde la derecha, cuente el número de 0 en la representación binaria, que es la potencia más alta de 2 que dividirá el número.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the highest power of 2
// which divides the given binary number
int highestPower(string str, int len)
{
 
    // To store the highest required power of 2
    int ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary string
    for (int i = len - 1; i >= 0; i--) {
        if (str[i] == '0')
            ans++;
        else
            break;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    string str = "100100";
    int len = str.length();
    cout << highestPower(str, len);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
// Function to return the highest power of 2
// which divides the given binary number
static int highestPower(String str, int len)
{
 
    // To store the highest required power of 2
    int ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary string
    for (int i = len - 1; i >= 0; i--)
    {
        if (str.charAt(i) == '0')
            ans++;
        else
            break;
    }
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "100100";
    int len = str.length();
    System.out.println(highestPower(str, len));
}
}
 
// This code is contributed by Code_Mech.

Python3

# Python3 implementation of the approach
 
# Function to return the highest power of 2
# which divides the given binary number
def highestPower(str, length):
 
    # To store the highest required power of 2
    ans = 0;
 
    # Counting number of consecutive zeros
    # from the end in the given binary string
    for i in range(length-1,-1,-1):
        if (str[i] == '0'):
            ans+=1;
        else:
            break;
    return ans;
 
# Driver code
def main():
    str = "100100";
    length = len(str);
    print(highestPower(str, length));
 
if __name__ == '__main__':
    main()
 
# This code contributed by PrinciRaj1992

C#

// C# implementation of the approach
using System;
     
class GFG
{
     
// Function to return the highest power of 2
// which divides the given binary number
static int highestPower(String str, int len)
{
 
    // To store the highest required power of 2
    int ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary string
    for (int i = len - 1; i >= 0; i--)
    {
        if (str[i] == '0')
            ans++;
        else
            break;
    }
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "100100";
    int len = str.Length;
    Console.WriteLine(highestPower(str, len));
}
}
 
/* This code contributed by PrinciRaj1992 */

PHP

<?php
// PHP implementation of the approach
 
// Function to return the highest power of 2
// which divides the given binary number
function highestPower($str, $len)
{
 
    // To store the highest required power of 2
    $ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary string
    for ($i = $len - 1; $i >= 0; $i--)
    {
        if ($str[$i] == '0')
            $ans++;
        else
            break;
    }
 
    return $ans;
}
 
// Driver code
$str = "100100";
$len = strlen($str);
echo highestPower($str, $len);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
// Javascript implementation of the approach
 
// Function to return the highest power of 2
// which divides the given binary number
function highestPower(str, len)
{
 
    // To store the highest required power of 2
    let ans = 0;
 
    // Counting number of consecutive zeros
    // from the end in the given binary string
    for (let i = len - 1; i >= 0; i--) {
        if (str[i] == '0')
            ans++;
        else
            break;
    }
 
    return ans;
}
 
// Driver code
    let str = "100100";
    let len = str.length;
    document.write(highestPower(str, len));
     
</script>
Producción

2

Complejidad de tiempo: O (n) donde n es el número de elementos en una array dada. Como estamos usando un bucle para atravesar N veces, nos costará O (N) tiempo 
Espacio auxiliar: O (1), ya que no estamos usando ningún espacio adicional.

Publicación traducida automáticamente

Artículo escrito por HadiHusain 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 *