Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find subsequences with maximum Bitwise AND and Bitwise OR in Python
When working with arrays and bitwise operations, we often need to find subsequences that maximize the sum of bitwise AND and OR operations. The key insight is that the maximum AND value comes from the largest single element, while the maximum OR value comes from combining all elements.
Problem Understanding
Given an array of n elements, we need to find two subsequences (possibly different) such that the sum of the bitwise AND of the first subsequence and bitwise OR of the second subsequence is maximized.
For example, with array A = [4, 6, 7, 2], the maximum AND is 7 (selecting only 7) and maximum OR is 7 (4 | 6 | 7 | 2 = 7), giving us a total of 14.
Algorithm Approach
The solution follows these steps:
Find the maximum element (gives maximum AND value)
Calculate OR of all elements (gives maximum OR value)
Return the sum of both values
Implementation
def get_max_sum(arr):
and_max = max(arr)
or_max = 0
for i in range(len(arr)):
or_max |= arr[i]
return and_max + or_max
# Test with example array
a = [4, 6, 7, 2]
result = get_max_sum(a)
print(f"Array: {a}")
print(f"Maximum sum: {result}")
Array: [4, 6, 7, 2] Maximum sum: 14
Step-by-Step Breakdown
Let's trace through the example with array [4, 6, 7, 2]:
def get_max_sum_detailed(arr):
# Maximum AND is the largest element
and_max = max(arr)
print(f"Maximum AND value: {and_max}")
# Maximum OR is OR of all elements
or_max = 0
for element in arr:
or_max |= element
print(f"OR so far: {or_max} (after including {element})")
total = and_max + or_max
print(f"Final result: {and_max} + {or_max} = {total}")
return total
a = [4, 6, 7, 2]
get_max_sum_detailed(a)
Maximum AND value: 7 OR so far: 4 (after including 4) OR so far: 6 (after including 6) OR so far: 7 (after including 7) OR so far: 7 (after including 2) Final result: 7 + 7 = 14
Alternative Implementation
Using Python's built-in functions for a more concise solution:
from functools import reduce
import operator
def get_max_sum_concise(arr):
and_max = max(arr)
or_max = reduce(operator.or_, arr, 0)
return and_max + or_max
# Test with different arrays
test_arrays = [[4, 6, 7, 2], [1, 3, 5], [8, 4, 2, 1]]
for arr in test_arrays:
result = get_max_sum_concise(arr)
print(f"Array {arr}: Maximum sum = {result}")
Array [4, 6, 7, 2]: Maximum sum = 14 Array [1, 3, 5]: Maximum sum = 12 Array [8, 4, 2, 1]: Maximum sum = 23
Why This Approach Works
The algorithm works because:
Maximum AND: AND operation can only turn bits off, so the maximum AND value is achieved by selecting the largest element alone
Maximum OR: OR operation turns bits on, so including all elements maximizes the OR value
These operations are independent, so we can optimize them separately
Conclusion
To find the maximum sum of bitwise AND and OR subsequences, simply take the maximum element for AND and compute the OR of all elements. This approach has O(n) time complexity and provides the optimal solution efficiently.
---