TikTok | OA 2023
Anonymous User
938

You are given an integer array arr.
For 0 <= i < arr.length, 0 <= j < arr.length and i != j return the maximum sum of xor(arr[i], arr[j]) and xor(other elements in between the indexes i and j), which can be expressed as (arr[i] ^ arr[j]) + (arr[i+1] ^ arr[i+2] ^ ... ^ arr[j-1]).

Example given:
[17, 5, 20, 1, 0, 24, 32], when (i, j) = (2, 6), max sum is 77

I tried a prefix sum approach to prevent calculating xor of subset of elemnts in between i and j over and over, but we still have an O(n^2) algorithm because I had to look up the prefix array for every i,j pair. Can we do better than O(n^2) ?

Comments (3)