Leetcode Problem 2935. Maximum Strong Pair XOR II

2935. Maximum Strong Pair XOR II

Leetcode Solutions

Bit by Bit Construction with Prefix Maps

  1. Initialize res to 0, which will hold the maximum XOR value.
  2. Iterate over the bits from the highest to the lowest (20 to 0 for this problem): a. Shift res left by 1 to make space for the next bit. b. Create two maps, pref and pref2, to store the minimum and maximum values with the current prefix. c. Iterate over the array and update pref and pref2 with the current elements. d. For each prefix x in pref, calculate the greedy guess y as res ^ 1 ^ x. e. If x >= y and y is in pref and pref[x] <= pref2[y] * 2, set the last bit of res to 1.
  3. Return res as the maximum XOR value.
UML Thumbnail

Trie-based Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...