Search⌘ K
AI Features

Genetic Difference

Understand how to compute the maximum genetic difference between family members and an external individual by leveraging trie traversal and bitwise XOR operations. Learn to build and traverse tries efficiently, apply depth-first search on graphs, and optimize queries to solve complex algorithmic problems involving bitwise tries.

Problem statement

In a family of NN people, individuals are numbered from 00 to N1N-1 and each individual has a unique binary representation. The genetic difference between two individuals is calculated as the XOR of their binary representations. For example, if two individuals in a family are denoted by 11 and 22, then their genetic difference is calculated as 1 XOR 2=31 \space XOR \space 2 = 3.

You're provided with an integer array parent, where parent[i]parent[i] is the parent of an individual ii. Also, the head (root) of the family does not have any parent, so the value of parent for root is 1-1. The scientists are now comparing the individuals in this family with a new external individual, and they wish to find the person in this family who is genetically most dissimilar to this new individual. But to limit the search space, they define the limit on the people to whom they'll be comparing the new individual.

You are given QQ queries in the format (nodei,valuei)(node_i, value_i). For every query, find the maximum genetic difference between valueivalue_i ...