Given a tree, and the weights of all the nodes and an integer x, the task is to find a node i such that weight[i] xor x is maximum.
x = 15
Node 1: 5 xor 15 = 10
Node 2: 10 xor 15 = 5
Node 3: 11 xor 15 = 4
Node 4: 8 xor 15 = 7
Node 5: 6 xor 15 = 9
Approach: Perform dfs on the tree and keep track of the node whose weighted xor with x gives the maximum value.
Below is the implementation of the above approach:
- Time Complexity : O(N).
In dfs, every node of the tree is processed once and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Therefore, the time complexity is O(N).
- Auxiliary Space : O(1).
Any extra space is not required, so the space complexity is constant.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the node whose xor with x gives minimum value
- Find the node whose absolute difference with X gives maximum value
- Find the node whose absolute difference with X gives minimum value
- All pairs whose xor gives unique prime
- Find a value whose XOR with given number is maximum
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum XOR with given value in the path from root to given node in the tree
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X
- Find the root of the sub-tree whose weighted sum XOR with X is maximum
- Count smaller numbers whose XOR with n produces greater value
- Range Query on array whose each element is XOR of index value and previous element
- Length of longest subsequence whose XOR value is odd
- Find Maximum XOR value of a sub-array of size k
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Number whose sum of XOR with given array range is maximum
- Given a set, find XOR of the XOR's of all subsets.
- Find XOR of two number without using XOR operator
- Find the node whose sum with X has maximum set bits
- Find the root of the sub-tree whose weighted sum XOR with X is minimum
- Find an N x N grid whose xor of every row and column is equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.