Find element with the maximum set bits in an array
Given an array arr[]. The task is to find an element from arr[] which has the maximum count of set bits.
Examples:
Input: arr[] = {10, 100, 1000, 10000}
Output: 1000
Binary(10) = 1010 (2 set bits)
Binary(100) = 1100100 (3 set bits)
Binary(1000) = 1111101000 (6 set bits)
Binary(10000) = 10011100010000 (5 set bits)
Input: arr[] = {3, 2, 4, 7, 1, 10, 5, 8, 9, 6}
Output: 7
Approach: Traverse the array and find the count of set bits in the current element and find the element with the maximum number of set bits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std; ;
int maxBitElement( int arr[], int n)
{
int num = 0, max = -1;
for ( int i = 0; i < n; i++) {
int cnt = __builtin_popcount(arr[i]);
if (cnt > max) {
max = cnt;
num = arr[i];
}
}
return num;
}
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 ;
}
|
Java
class GFG {
static int maxBitElement( int arr[], int n)
{
int num = 0 , max = - 1 ;
for ( int i = 0 ; i < n; i++) {
int cnt = Integer.bitCount(arr[i]);
if (cnt > max) {
max = cnt;
num = arr[i];
}
}
return num;
}
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
def maxBitElement(arr, n):
num = 0
max = - 1
for i in range (n):
cnt = bin (arr[i]).count( '1' )
if (cnt > max ):
max = cnt
num = arr[i]
return num
if __name__ = = '__main__' :
arr = [ 3 , 2 , 4 , 7 , 1 ,
10 , 5 , 8 , 9 , 6 ]
n = len (arr)
print (maxBitElement(arr, n))
|
C#
using System;
class GFG
{
static int maxBitElement( int []arr, int n)
{
int num = 0, max = -1;
for ( int i = 0; i < n; i++)
{
int cnt = BitCount(arr[i]);
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;
}
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));
}
}
|
PHP
<?php
function maxBitElement( $arr , $n )
{
$num = 0; $max = -1;
for ( $i = 0; $i < $n ; $i ++)
{
$cnt = BitCount( $arr [ $i ]);
if ( $cnt > $max )
{
$max = $cnt ;
$num = $arr [ $i ];
}
}
return $num ;
}
function BitCount( $n )
{
$count = 0;
while ( $n != 0)
{
$count ++;
$n &= ( $n - 1);
}
return $count ;
}
$arr = array (3, 2, 4, 7, 1, 10, 5, 8, 9, 6 );
$n = count ( $arr );
echo (maxBitElement( $arr , $n ));
?>
|
Javascript
<script>
function maxBitElement(arr, n)
{
let num = 0, max = -1;
for (let i = 0; i < n; i++) {
let cnt = BitCount(arr[i]);
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;
}
let arr = [ 3, 2, 4, 7, 1, 10, 5, 8, 9, 6 ];
let n = arr.length;
document.write(maxBitElement(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
26 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...