BackmediumTreesCredAccenture

Bounded Range Segment Evaluator 7 Solution

Problem Statement

You are given an integer array nums of length N. You must answer Q independent queries. Each query provides two indices L and R (1‑based, inclusive) that define a sub‑array nums[L..R]. For that sub‑array, determine the maximum possible value of nums[i] XOR nums[j] where L ≤ i < j ≤ R. Return the answer for each query in the order they are given. The required solution should run efficiently for up to 10⁵ elements and 10⁵ queries, which typically involves building a binary trie (bitwise prefix tree) for the numbers in the current range or using an offline technique such as Mo's algorithm combined with a trie.

Example 1
Input
5 3 10 5 25 2 2 1 3 2 5
Output
15 28

Explanation: Query 1 (1‑3): sub‑array = [3,10,5]. Pairwise XORs are 3⊕10=9, 3⊕5=6, 10⊕5=15 → maximum = 15. Query 2 (2‑5): sub‑array = [10,5,25,2]. Pairwise XORs are 10⊕5=15, 10⊕25=19, 10⊕2=8, 5⊕25=28, 5⊕2=7, 25⊕2=27 → maximum = 28.

Example 2
Input
4 0 1 2 3 3 1 4 1 2 3 4
Output
3 1 1

Explanation: Query 1 (1‑4): all numbers [0,1,2,3]; the largest XOR is 0⊕3 = 3 (also 1⊕2 = 3). Query 2 (1‑2): sub‑array [0,1]; only pair is 0⊕1 = 1. Query 3 (3‑4): sub‑array [2,3]; only pair is 2⊕3 = 1.

Example 3
Input
6 8 1 2 12 7 6 1 2 5
Output
14

Explanation: Query (2‑5): sub‑array = [1,2,12,7]. Pairwise XORs: 1⊕2=3, 1⊕12=13, 1⊕7=6, 2⊕12=14, 2⊕7=5, 12⊕7=11. The maximum is 14.

Constraints

  • 1 ≤ N ≤ 10⁵
  • 1 ≤ Q ≤ 10⁵
  • 0 ≤ nums[i] ≤ 10⁹
  • 1 ≤ L < R ≤ N
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Bounded Range Segment Evaluator 7 — Problem Statement & Solution Guide

TreesMediumTrie XOR Maximum
TimeO((N + Q) * log N * log MAX_VAL) // ≈ O((N+Q)·log N) for 32‑bit ints
|
SpaceO(N * log MAX_VAL) // persistent or segment‑tree tries

Problem Description

You are given an integer array nums of length N. You must answer Q independent queries. Each query provides two indices L and R (1‑based, inclusive) that define a sub‑array nums[L..R]. For that sub‑array, determine the maximum possible value of nums[i] XOR nums[j] where L ≤ i < j ≤ R. Return the answer for each query in the order they are given. The required solution should run efficiently for up to 10⁵ elements and 10⁵ queries, which typically involves building a binary trie (bitwise prefix tree) for the numbers in the current range or using an offline technique such as Mo's algorithm combined with a trie.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Bounded Range Segment Evaluator 7"

medium

WHY DOES IT MATTER?

Maximum XOR queries are a classic example of bitwise optimization where the naive quadratic scan is impossible at scale. Mastering the binary trie pattern unlocks efficient solutions for many bit‑manipulation problems, especially those involving pairwise combinations within dynamic ranges.

OPTIMIZATION CHALLENGE

The breakthrough is realizing that the XOR maximization can be performed greedily bit‑by‑bit using a trie, and that a range can be represented by O(log N) disjoint segments. Merging these small tries on‑the‑fly (or using persistence) reduces the per‑query work from linear to logarithmic.

REAL-WORLD CONNECTION

Think of a distributed cache that stores hashed keys; finding two keys with the most divergent hash bits within a shard is analogous to max‑XOR. Segment‑tree‑of‑tries mirrors how a system might keep per‑shard summaries (tries) to answer such divergence queries without scanning every key.

When coding, build the trie as an array of two child indices and a count; this makes deletions for updates trivial. Also, pre‑allocate enough nodes (N·log MAX_VAL) to avoid dynamic allocations that kill performance under tight time limits.

COMPLEXITY AT A GLANCE

⏱ Time:O((N + Q) * log N * log MAX_VAL) // ≈ O((N+Q)·log N) for 32‑bit ints
💾 Space:O(N * log MAX_VAL) // persistent or segment‑tree tries

Core Theory — Why This Approach?

The maximum XOR of any two numbers inside a sub‑array can be found by exploiting the binary trie (also called a prefix tree) that stores the bit representation of the numbers. A naive O((R‑L)^2) scan quickly becomes infeasible when N and Q are up to 2·10^5 because each query could touch O(N) elements. The optimal paradigm combines divide‑and‑conquer or segment‑tree decomposition with a bitwise trie: each node of the segment tree represents a contiguous segment of the original array and holds a trie of all numbers in that segment. A query [L,R] is answered by merging O(log N) tries (the nodes that exactly cover the interval) and then walking the merged view to find the pair that yields the highest XOR. Because the trie depth is bounded by the word size (≤31 for 32‑bit ints), each merge step and each query run in O(log MAX_VAL·log N) ≈ O(log N) time, giving an overall O((N+Q)·log N) solution. Persistent tries can achieve the same bound with O(log MAX_VAL) per query by storing a root for each prefix and answering a range query via two roots (R and L‑1). Both approaches avoid the quadratic blow‑up and keep memory linear in N·log MAX_VAL.

Interview Questions on This Problem

Q1How would you modify the segment‑tree‑of‑tries solution to support point updates (changing nums[i]) while still answering max‑XOR range queries efficiently?

Replace each static trie in the segment tree with a mutable binary trie that supports insert and delete in O(log MAX_VAL). On an update, traverse the segment‑tree path to the leaf, deleting the old value and inserting the new one at each node’s trie. Queries remain O(log N·log MAX_VAL).

Q2Explain why a persistent binary trie built over prefix arrays can answer max‑XOR queries for any range [L,R] using only two roots.

A persistent trie stores the multiset of numbers up to each index. The trie for prefix R contains all numbers in [1,R]; the trie for prefix L‑1 contains numbers in [1,L‑1]. By walking both tries simultaneously, we can consider only numbers present in R’s trie but not fully cancelled by L‑1’s trie, effectively restricting to [L,R]. The walk chooses opposite bits to maximize XOR, yielding the answer in O(log MAX_VAL).

Q3In a high‑throughput system where Q can be 10^6 and latency per query must be sub‑millisecond, which data structure would you choose and why?

A persistent trie is preferable because it answers each query in O(log MAX_VAL) without any per‑query merging, leading to a very small constant factor. Pre‑computing the persistent roots once (O(N·log MAX_VAL) time) allows each query to be answered by a single walk, meeting strict latency requirements.

Examples

Example 1

Input

5
3 10 5 25 2
2
1 3
2 5

Output

15
28

Explanation: Query 1 (1‑3): sub‑array = [3,10,5]. Pairwise XORs are 3⊕10=9, 3⊕5=6, 10⊕5=15 → maximum = 15. Query 2 (2‑5): sub‑array = [10,5,25,2]. Pairwise XORs are 10⊕5=15, 10⊕25=19, 10⊕2=8, 5⊕25=28, 5⊕2=7, 25⊕2=27 → maximum = 28.

Example 2

Input

4
0 1 2 3
3
1 4
1 2
3 4

Output

3
1
1

Explanation: Query 1 (1‑4): all numbers [0,1,2,3]; the largest XOR is 0⊕3 = 3 (also 1⊕2 = 3). Query 2 (1‑2): sub‑array [0,1]; only pair is 0⊕1 = 1. Query 3 (3‑4): sub‑array [2,3]; only pair is 2⊕3 = 1.

Example 3

Input

6
8 1 2 12 7 6
1
2 5

Output

14

Explanation: Query (2‑5): sub‑array = [1,2,12,7]. Pairwise XORs: 1⊕2=3, 1⊕12=13, 1⊕7=6, 2⊕12=14, 2⊕7=5, 12⊕7=11. The maximum is 14.

Constraints

  • 1 ≤ N ≤ 10⁵
  • 1 ≤ Q ≤ 10⁵
  • 0 ≤ nums[i] ≤ 10⁹
  • 1 ≤ L < R ≤ N

Optimal Approach & Strategy

Build a segment tree where each node stores a binary trie of its segment; answer a query by merging O(log N) tries and greedily walking the merged view to compute the max XOR in O(log MAX_VAL·log N) time.

Brute Force Approach

Iterate over all pairs (i, j) inside the query range and compute nums[i] XOR nums[j], keeping the maximum; this is O((R‑L+1)^2) per query.

Verified Code Solutions

JavaScript Solution
Time: O((N + Q) * log N * log MAX_VAL) // ≈ O((N+Q)·log N) for 32‑bit ints
function solution(nums) {
   let sum = 0;
   for (let num of nums) {
       if (num >= 0) {
           sum += num;
       }
   }
   return sum;
}

Asked in Top Tech Interviews

CredAccenture

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.