Maximum set bit sum in array without considering adjacent elements
Last Updated :
11 Jul, 2025
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:
- First of all, find the total number of set bits for every element of the array and store them in a different array or the same array(to avoid using extra space).
- Now, the problem is reduced to find the maximum sum in the array such that no two elements are adjacent.
- Loop for all elements in arr[] and maintain two sums incl and excl where incl = Max sum including the previous element and excl = Max sum excluding the previous element.
- Max sum excluding the current element will be max(incl, excl) and max sum including the current element will be excl + current element (Note that only excl is considered because elements cannot be adjacent).
- At the end of the loop return max of incl and excl.
Below is the implementation of the above approach:
C++
// C++ program to maximum set bit sum in array
// without considering adjacent elements
#include<bits/stdc++.h>
using namespace std;
// Function to count total number
// of set bits in an integer
int bit(int n)
{
int count = 0;
while(n)
{
count++;
n = n & (n - 1);
}
return count;
}
// Maximum sum of set bits
int maxSumOfBits(int arr[], int n)
{
// Calculate total number of
// set bits for every element
// of the array
for(int i = 0; i < n; i++)
{
// find total set bits for
// each number and store
// back into the array
arr[i] = bit(arr[i]);
}
int incl = arr[0];
int excl = 0;
int excl_new;
for (int i = 1; i < n; i++)
{
// current max excluding i
excl_new = (incl > excl) ?
incl : excl;
// current max including i
incl = excl + arr[i];
excl = excl_new;
}
// return max of incl and excl
return ((incl > excl) ?
incl : excl);
}
// Driver code
int main()
{
int arr[] = {1, 2, 4, 5,
6, 7, 20, 25};
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSumOfBits(arr, n);
return 0;
}
Java
// Java program to maximum set bit sum in array
// without considering adjacent elements
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Function to count total number
// of set bits in an integer
static int bit(int n)
{
int count = 0;
while(n > 0)
{
count++;
n = n & (n - 1);
}
return count;
}
// Maximum sum of set bits
static int maxSumOfBits(int arr[], int n)
{
// Calculate total number of set bits
// for every element of the array
for(int i = 0; i < n; i++)
{
// find total set bits for
// each number and store
// back into the array
arr[i] = bit(arr[i]);
}
int incl = arr[0];
int excl = 0;
int excl_new;
for (int i = 1; i < n; i++)
{
// current max excluding i
excl_new = (incl > excl) ?
incl : excl;
// current max including i
incl = excl + arr[i];
excl = excl_new;
}
// return max of incl and excl
return ((incl > excl) ?
incl : excl);
}
// Driver code
public static void main(String args[])
{
int arr[] = {1, 2, 4, 5,
6, 7, 20, 25};
int n = arr.length;
System.out.print(maxSumOfBits(arr, n));
}
}
// This code is contributed
// by Subhadeep
Python3
# Python3 program to maximum set bit sum in
# array without considering adjacent elements
# Function to count total number
# of set bits in an integer
def bit(n):
count = 0
while(n):
count += 1
n = n & (n - 1)
return count
# Maximum sum of set bits
def maxSumOfBits(arr, n):
# Calculate total number of set bits
# for every element of the array
for i in range( n):
# find total set bits for each
# number and store back into the array
arr[i] = bit(arr[i])
incl = arr[0]
excl = 0
for i in range(1, n) :
# current max excluding i
if incl > excl:
excl_new = incl
else:
excl_new = excl
# current max including i
incl = excl + arr[i];
excl = excl_new
# return max of incl and excl
if incl > excl:
return incl
else :
return excl
# Driver code
if __name__ == "__main__":
arr = [1, 2, 4, 5,
6, 7, 20, 25]
n = len(arr)
print (maxSumOfBits(arr, n))
# This code is contributed by ita_c
C#
// C# program to maximum set bit sum in array
// without considering adjacent elements
using System;
class GFG
{
// Function to count total number
// of set bits in an integer
static int bit(int n)
{
int count = 0;
while(n > 0)
{
count++;
n = n & (n - 1);
}
return count;
}
// Maximum sum of set bits
static int maxSumOfBits(int []arr, int n)
{
// Calculate total number of set bits
// for every element of the array
for(int i = 0; i < n; i++)
{
// find total set bits for
// each number and store
// back into the array
arr[i] = bit(arr[i]);
}
int incl = arr[0];
int excl = 0;
int excl_new;
for (int i = 1; i < n; i++)
{
// current max excluding i
excl_new = (incl > excl) ?
incl : excl;
// current max including i
incl = excl + arr[i];
excl = excl_new;
}
// return max of incl and excl
return ((incl > excl) ?
incl : excl);
}
// Driver code
public static void Main()
{
int []arr = {1, 2, 4, 5,
6, 7, 20, 25};
int n = arr.Length;
Console.WriteLine(maxSumOfBits(arr, n));
}
}
// This code is contributed
// by chandan_jnu.
PHP
<?php
// PHP program to maximum set bit sum in array
// without considering adjacent elements
// Function to count total number
// of set bits in an integer
function bit($n)
{
$count = 0;
while($n)
{
$count++;
$n = $n & ($n - 1);
}
return $count;
}
// Maximum sum of set bits
function maxSumOfBits($arr, $n)
{
// Calculate total number of
// set bits for every element
// of the array
for( $i = 0; $i < $n; $i++)
{
// find total set bits for
// each number and store
// back into the array
$arr[$i] = bit($arr[$i]);
}
$incl = $arr[0];
$excl = 0;
$excl_new;
for ($i = 1; $i < $n; $i++)
{
// current max excluding i
$excl_new = ($incl > $excl) ?
$incl : $excl;
// current max including i
$incl = $excl + $arr[$i];
$excl = $excl_new;
}
// return max of incl and excl
return (($incl > $excl) ?
$incl : $excl);
}
// Driver code
$arr = array(1, 2, 4, 5,
6, 7, 20, 25);
$n = sizeof($arr) / sizeof($arr[0]);
echo maxSumOfBits($arr, $n);
#This Code is Contributed by ajit
?>
JavaScript
<script>
// Javascript program to maximum
// set bit sum in array
// without considering adjacent elements
// Function to count total number
// of set bits in an integer
function bit(n)
{
let count = 0;
while(n > 0)
{
count++;
n = n & (n - 1);
}
return count;
}
// Maximum sum of set bits
function maxSumOfBits(arr, n)
{
// Calculate total number of set bits
// for every element of the array
for(let i = 0; i < n; i++)
{
// find total set bits for
// each number and store
// back into the array
arr[i] = bit(arr[i]);
}
let incl = arr[0];
let excl = 0;
let excl_new;
for (let i = 1; i < n; i++)
{
// current max excluding i
excl_new = (incl > excl) ? incl : excl;
// current max including i
incl = excl + arr[i];
excl = excl_new;
}
// return max of incl and excl
return ((incl > excl) ? incl : excl);
}
let arr = [1, 2, 4, 5, 6, 7, 20, 25];
let n = arr.length;
document.write(maxSumOfBits(arr, n));
</script>
complexity Analysis:
- Time Complexity: O(Nlogn)
- Auxiliary Space: O(1)
Note: Above code can be optimised to O(N) using __builtin_popcount function to count set bits in O(1) time.
Similar Reads
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 min read
Maximum sum of Array formed by replacing each element with sum of adjacent elements Given an array arr[] of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.Examples: Input: arr = [4, 2, 1, 3] Output: 23 Explanation: Replacing each element of the original array with the sum of adjacent
9 min read
Reduce given Array to 0 by maximising sum of chosen elements Given an array arr[] containing N positive integers, the task is to maximize the array sum when after every sum operation all the remaining array elements decrease by 1. Note: The value of an array element does not go below 0. Examples: Input: arr[] = {6, 2, 4, 5} Output: 12 Explanation: Add 6 initi
9 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
Find maximum sum from top to bottom row with no adjacent diagonal elements Given a matrix A[][] of N * M, the task is to find the maximum sum from the top row to the bottom row after selecting one element from each row with no adjacent diagonal element. Examples: Input: A = { {1, 2, 3, 4}, {8, 7, 6, 5}, {10, 11, 12, 13} } Output: 25 Explanation: Selected elements to give m
8 min read
Sum of array elements whose count of set bits are unique Given an array arr[] consisting of N positive integers, the task is to find the sum of all array elements having a distinct count of set bits in the array. Examples: Input: arr[] = {8, 3, 7, 5, 3}Output: 15Explanation:The count of set bits in each array of elements is: arr[0] = 8 = (1000)2, has 1 se
7 min read