Xor profit problem

I have implemented this problem in O(n^2).
Can we decrease its time complexity further??

When considering XOR of two numbers the maximum value of XOR can be observed in cases like 7(binary:111) and 8(binary:1000) Such cases are obtained when the numbers are consecutive, therefore, this problem can be implemented in O(n) by checking only consecutive numbers’ xor value.

I hope this was helpful.

Thank You