Reorder Array in a special way to maximize score

You are given an array A containing positive integers (1 <= A[i] <= 10^9)
The array score is calculated based on the following algorithm:

  • Take first two elements of array (a1 and a2) and replace them with a single element with value = a1|a2 - a1&a2
  • Repeat the process until there is a single integer left in array A
    The last remaining element would be the score of the array
    | represents bitwise OR and & represents bitwise AND
    The task is to find the maximum score of the array we can obtain by reordering the elements of the array in any way.

Example:
if A = [1,2,3]
after applying algorithm 1st time A = [3,3] (a1=1 and a2=2)
in next iteration A = [0] (a1=3 and a2=3) so score is 0 which is maximumum possible in this case hence answer is 0

another example if A = [1,2] answer = 3

How to solve this question or how to proceed with such questions?
Code in javascript would be helpful, but other languages are also welcome.

Comments (2)