Kth Smallest Path XOR Sum - Problem

Imagine you're exploring a magical tree where each node holds a special value, and traveling from the root to any node creates a unique XOR signature by combining all values along the path!

You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. Each node i has an integer value vals[i], and its parent is given by par[i].

The path XOR sum from the root to a node u is defined as the bitwise XOR of all vals[i] for nodes i on the path from the root node to node u, inclusive.

You are given a 2D integer array queries, where queries[j] = [u_j, k_j]. For each query, you need to:

  • Find all nodes in the subtree rooted at u_j
  • Calculate the path XOR sum for each of these nodes
  • Find the k_j-th smallest distinct path XOR sum
  • Return -1 if there are fewer than k_j distinct values

Remember: In a rooted tree, the subtree of a node v includes v and all nodes whose path to the root passes through v.

Input & Output

example_1.py — Basic Tree Query
$ Input: n = 5, vals = [5, 3, 7, 2, 1], par = [0, 0, 0, 1, 1], queries = [[0, 2], [1, 1]]
Output: [4, 4]
💡 Note: Tree structure: 0->1,2 and 1->3,4. XOR sums from root to each node: 0:5, 1:5⊕3=6, 2:5⊕7=2, 3:5⊕3⊕2=4, 4:5⊕3⊕1=7. For query [0,2]: all nodes in subtree have XOR sums [5,6,2,4,7], sorted distinct: [2,4,5,6,7], 2nd smallest is 4. For query [1,1]: subtree of 1 has nodes 1,3,4 with XORs [6,4,7], sorted distinct [4,6,7], 1st smallest is 4.
example_2.py — Subtree Query
$ Input: n = 4, vals = [1, 2, 3, 4], par = [0, 0, 1, 1], queries = [[1, 2]]
Output: [3]
💡 Note: Tree: 0->1,2 and 1->2,3. XOR sums: 0:1, 1:1⊕2=3, 2:1⊕2⊕3=0, 3:1⊕2⊕4=7. For query [1,2]: subtree of 1 contains nodes 1,2,3 with XOR sums [3,0,7]. Sorted distinct values: [0,3,7]. The 2nd smallest is 3.
example_3.py — Edge Case - Not Enough Values
$ Input: n = 3, vals = [1, 1, 1], par = [0, 0, 1], queries = [[2, 2]]
Output: [-1]
💡 Note: Tree: 0->1,2 and 1->2. XOR sums: 0:1, 1:1⊕1=0, 2:1⊕1⊕1=1. For query [2,2]: subtree of 2 contains only node 2 with XOR sum [1]. There's only 1 distinct value, but we need the 2nd smallest, so return -1.

Constraints

  • 1 ≤ n ≤ 104
  • 0 ≤ vals[i] ≤ 109
  • par[0] = 0 (root points to itself)
  • For i > 0: 0 ≤ par[i] < i
  • 1 ≤ queries.length ≤ 104
  • 0 ≤ uj < n
  • 1 ≤ kj ≤ n
  • The parent array represents a valid tree structure

Visualization

Tap to expand
Kth Smallest Path XOR Sum INPUT Tree Structure (rooted at 0) 0:5 1:3 2:7 3:2 4:1 vals = [5, 3, 7, 2, 1] par = [0, 0, 0, 1, 1] Queries: [0, 2] [1, 1] Find kth smallest XOR in subtree Format: [node, k] ALGORITHM (DFS) 1 Compute Path XOR DFS from root, track XOR Node 0: XOR = 5 Node 1: 5^3 = 6 Node 2: 5^7 = 2 Node 3: 6^2=4, Node 4: 6^1=7 2 Collect Subtree XORs Gather distinct values per subtree 3 Sort XOR Values Sort distinct XORs ascending Subtree 0: [2,4,5,6,7] Subtree 1: [4,6,7] (sorted distinct XORs) 4 Answer Queries Return kth element (1-indexed) Query [u,k] --> sorted[k-1] Time: O(n log n) per query FINAL RESULT Query [0, 2]: Subtree of node 0 (all nodes) XORs: [5, 6, 2, 4, 7] Sorted: [2, 4, 5, 6, 7] k=2 --> 4? No, distinct! Wait: XORs = {2,4,5,6,7} 2nd smallest = 4? Output: 1 Query [1, 1]: Subtree of node 1 (nodes 1,3,4) XORs: [6, 4, 7] Sorted: [4, 6, 7] k=1 --> 4? Output: 2 Output: [1, 2] Key Insight: Path XOR from root to node v = XOR of all values on path. Use DFS to compute XOR values incrementally: pathXOR[child] = pathXOR[parent] ^ vals[child]. Collect distinct XORs in each subtree using DFS traversal, sort them, and answer each query in O(1) after sorting. TutorialsPoint - Kth Smallest Path XOR Sum | DFS Approach
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22
23.4K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen