1. All Pairs Quadratic Search
Intuition
Use two nested loops to check all pairs (i, j). Compute nums[i] ^ nums[j] and update maxXor.
When to apply: When n <= 2000.
Algorithm
- Step 1: Set maxXor = 0.
- Step 2: Loop i from 0 to n - 1.
- Step 3: Loop j from i to n - 1.
- Step 4: maxXor = max(maxXor, nums[i] ^ nums[j]).
- Step 5: Return maxXor.
#include <iostream>#include <vector>#include <algorithm> using namespace std; int findMaximumXORBrute(const vector<int>& nums) { int maxXor = 0; int n = nums.size(); for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { maxXor = max(maxXor, nums[i] ^ nums[j]); } } return maxXor;} int main() { vector<int> nums = {3, 10, 5, 25, 2, 8}; cout << "Brute: " << findMaximumXORBrute(nums) << endl; // 28 return 0;}Time & Space Complexity
- Time complexity:
O(n^2) - Space complexity:
O(1)