Given array arr of length n, we define function f(arr) as- if n=1, f(arr) = arr[0]; else, f(arr) = f(arr[0] ^ arr[1], arr[1] ^ arr[2]...., arr[n-2] ^ arr[n-1])
where ^ is bitwise XOR operator.
For example, arr = [1, 2, 4, 8], n = 4
f(1, 2, 4, 8) = f(1^2, 2^4, 4^8) = f(3,6,12) = f(3^6,6^12) = f( 5, 10) = f(5^10) = f(15) = 15.
You need to answer q queries, each query you are given two integers l and r. For each, what is the maximum of f() for all continuous subsegments of the array from l to r.

for eg.
Given Array = [1,2,4,8,16,32]
Given l = 1 and r = 4

Ans = Max(f(2), f(4), f(8), f(16), f(2,4), f(4,8), f(8,16), f(2,4,8), f(4,8,16), f(2,4,8,16))

Edit: Here is a video solution I found on YT-

Comments (6)