- Related Questions & Answers
- Bitwise AND and OR in Arduino
- Maximum subset with bitwise OR equal to k in C++
- Maximum Bitwise AND pair from given range in C++
- Maximum steps to transform 0 to X with bitwise AND in C++
- Bitwise and (or &) of a range in C++
- Python Bitwise Operators
- What is Bitwise AND in C++?
- Triples with Bitwise AND Equal To Zero in C++
- C# Bitwise and Bit Shift Operators
- What is Bitwise OR in C++?
- Maximum bitwise AND value of a pair in an array in C++
- Bitwise AND of Numbers Range in C++
- Bitwise OR (or - ) of a range in C++
- What is JavaScript Bitwise OR (|) Operator?
- Count pairs with Bitwise-AND as even number in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array of n elements, we have to show the maximum sum by choosing two subsequences of the array (they may or may not be different) so that the sum of bit-wise AND operation of all elements of the first subsequence and bit-wise OR operation of all the elements of the second subsequence is maximum.

So, if the input is like A = {4, 6, 7, 2}, then the output will be 14 as we are getting maximum AND value by selecting 7 only and maximum OR value by selecting all (4 | 6 | 7 | 2) = 7. So, the result will be 7 + 7 = 14.

To solve this, we will follow these steps −

and_max := maximum of arr

or_max := 0

for i in range 0 to size of arr, do

or_max := or_max OR arr[i]

return and_max + or_max

Let us see the following implementation to get better understanding −

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 a = [4,6,7,2] print(get_max_sum(a))

[4,6,7,2]

Output

14

Advertisements