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++
// 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 ?> |
7
Recommended Posts:
- Find maximum sum taking every Kth element in the array
- Find the maximum element in an array which is first increasing and then decreasing
- Find maximum value of the last element after reducing the array with given operations
- Maximum element in an array such that its previous and next element product is maximum
- Find the node whose sum with X has maximum set bits
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Find last element after deleting every second element in array of n integers
- Type of array and its maximum element
- Maximum element in a sorted and rotated array
- Remove all occurrences of any element for maximum array sum
- Sum and Product of minimum and maximum element of an Array
- Maximum distance between two occurrences of same element in array
- Maximum difference between first and last indexes of an element in array
- Find an element in array such that sum of left array is equal to sum of right array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



