Encuentre el elemento con los bits máximos establecidos en una array

Dada una array arr[] . La tarea es encontrar un elemento de arr[] que tenga el recuento máximo de bits establecidos.
Ejemplos: 
 

Entrada: arr[] = {10, 100, 1000, 10000} 
Salida: 1000 
Binario(10) = 1010 (2 bits establecidos) 
Binario(100) = 1100100 (3 bits establecidos) 
Binario(1000) = 1111101000 (6 bits establecidos ) 
Binario (10000) = 10011100010000 (5 bits establecidos)
Entrada: arr[] = {3, 2, 4, 7, 1, 10, 5, 8, 9, 6} 
Salida:
 

Enfoque: recorra la array y encuentre el recuento de bits establecidos en el elemento actual y encuentre el elemento con el número máximo de bits establecidos.
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 element from the array
// which has the maximum set bits
int maxBitElement(int arr[], int n)
{
 
    // To store the required element and
    // the maximum set bits so far
    int num = 0, max = -1;
 
    for (int i = 0; i < n; i++) {
 
        // Count of set bits in
        // the current element
        int cnt = __builtin_popcount(arr[i]);
 
        // Update the max
        if (cnt > max) {
            max = cnt;
            num = arr[i];
        }
    }
    return num;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 2, 4, 7, 1, 10, 5, 8, 9, 6 };
    int n = sizeof(arr)/ sizeof(arr[0]) ;
     
    cout << maxBitElement(arr, n) << endl;
     
return 0 ;
 
// This code is contributed by AnkitRai01
}

Java

// Java implementation of the approach
class GFG {
 
    // Function to return the element from the array
    // which has the maximum set bits
    static int maxBitElement(int arr[], int n)
    {
 
        // To store the required element and
        // the maximum set bits so far
        int num = 0, max = -1;
 
        for (int i = 0; i < n; i++) {
 
            // Count of set bits in
            // the current element
            int cnt = Integer.bitCount(arr[i]);
 
            // Update the max
            if (cnt > max) {
                max = cnt;
                num = arr[i];
            }
        }
        return num;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 3, 2, 4, 7, 1, 10, 5, 8, 9, 6 };
        int n = arr.length;
        System.out.print(maxBitElement(arr, n));
    }
}

Python3

# Python 3 implementation of the approach
 
# Function to return the element from the array
# which has the maximum set bits
def maxBitElement(arr, n):
     
    # To store the required element and
    # the maximum set bits so far
    num = 0
    max = -1
 
    for i in range(n):
         
        # Count of set bits in
        # the current element
        cnt = bin(arr[i]).count('1')
 
        # Update the max
        if (cnt > max):
            max = cnt
            num = arr[i]
    return num
 
# Driver code
if __name__ == '__main__':
    arr = [3, 2, 4, 7, 1,
          10, 5, 8, 9, 6]
    n = len(arr)
     
    print(maxBitElement(arr, n))
     
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the element from the array
    // which has the maximum set bits
    static int maxBitElement(int []arr, int n)
    {
 
        // To store the required element and
        // the maximum set bits so far
        int num = 0, max = -1;
 
        for (int i = 0; i < n; i++)
        {
 
            // Count of set bits in
            // the current element
            int cnt = BitCount(arr[i]);
 
            // Update the max
            if (cnt > max)
            {
                max = cnt;
                num = arr[i];
            }
        }
        return num;
    }
    static int BitCount(int n)
    {
        int count = 0;
        while (n != 0)
        {
            count++;
            n &= (n - 1);
        }
        return count;
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 3, 2, 4, 7, 1, 10, 5, 8, 9, 6 };
        int n = arr.Length;
        Console.Write(maxBitElement(arr, n));
    }
}
 
// This code contributed by Rajput-Ji

PHP

<?php
// PHP implementation of the approach
 
// Function to return the element from
// the array which has the maximum set bits
function maxBitElement($arr, $n)
{
 
    // To store the required element and
    // the maximum set bits so far
    $num = 0; $max = -1;
 
    for ($i = 0; $i < $n; $i++)
    {
 
        // Count of set bits in
        // the current element
        $cnt = BitCount($arr[$i]);
 
        // Update the max
        if ($cnt > $max)
        {
            $max = $cnt;
            $num = $arr[$i];
        }
    }
    return $num;
}
 
function BitCount($n)
{
    $count = 0;
    while ($n != 0)
    {
        $count++;
        $n &= ($n - 1);
    }
    return $count;
}
     
// Driver code
$arr = array(3, 2, 4, 7, 1, 10, 5, 8, 9, 6 );
$n = count($arr);
echo(maxBitElement($arr, $n));
 
// This code contributed by PrinciRaj1992
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the element from the array
// which has the maximum set bits
function maxBitElement(arr, n)
{
 
    // To store the required element and
    // the maximum set bits so far
    let num = 0, max = -1;
 
    for (let i = 0; i < n; i++) {
 
        // Count of set bits in
        // the current element
        let cnt = BitCount(arr[i]);
 
        // Update the max
        if (cnt > max) {
            max = cnt;
            num = arr[i];
        }
    }
    return num;
}
 
function BitCount(n)
{
    let count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
     
// Driver code
    let arr = [ 3, 2, 4, 7, 1, 10, 5, 8, 9, 6 ];
    let n = arr.length;
     
    document.write(maxBitElement(arr, n));
 
</script>
Producción: 

7

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

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 *