Find element with the maximum set bits in an array Last Updated : 26 Nov, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ?> 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> Output: 7 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum set bit sum in array without considering adjacent elements Anonymous Improve Article Tags : DSA Similar Reads Find the element having maximum premutiples in the array Given an array arr[], the task is to find the element which has the maximum number of pre-multiples present in the set. For any index i, pre-multiple is the number that is multiple of i and is present before the ith index of the array. Also, print the count of maximum multiples of that element in th 8 min read Maximum set bit sum in array without considering adjacent elements Given an array of integers arr[]. The task is to find the maximum sum of set bits(of the array elements) without adding the set bits of adjacent elements of the array. Examples: Input : arr[] = {1, 2, 4, 5, 6, 7, 20, 25} Output : 9 Input : arr[] = {5, 7, 9, 5, 13, 7, 20, 25} Output : 11 Approach: Fi 8 min read Maximize total set bits of elements in N sized Array with sum M Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M. Examples: Input: N = 1, M = 15Output: 4Explanation: Since N =1, 8 min read Split Array into maximum Subsets with same bitwise AND Given an array arr[] of size N, the task is to find the maximum number of subsets the array can be split such that the bitwise AND of the subsets is the same. Examples: Input: N = 4, arr[] = {1, 5, 2, 8}Output: 2Explanation:1st subset -> {1, 8}; bitwise AND = 0 2nd subset -> {2, 5}; bitwise AN 10 min read Maximum in array which is at-least twice of other elements Given an array of integers of length n. Our task is to return the index of the max element if the it is at least twice as much as every other number in the array. If the max element does not satisfy the condition return -1. Examples: Input : arr = {3, 6, 1, 0} Output : 1 Here, 6 is the largest integ 7 min read Count subsequences for every array element in which they are the maximum Given an array arr[] consisting of N unique elements, the task is to generate an array B[] of length N such that B[i] is the number of subsequences in which arr[i] is the maximum element. Examples: Input: arr[] = {2, 3, 1}Output: {2, 4, 1}Explanation: Subsequences in which arr[0] ( = 2) is maximum a 11 min read Like