# Find the color of given node in an infinite binary tree

Given an infinitely long binary tree having a pattern as below:

```                      1
/   \
2       3
/  \     /   \
4    5   6     7
/  \  / \ / \   / \
......................
```

Also given an array arr of size N and a number K. The task is to color all the subtrees of node arr[i] with colour i (1 <= i <= N) one after another. Find the colour of node K after colouring all subtrees.

Note:- Initially all nodes have color 0.

Input :arr[] = {3, 2, 1, 3}, K = 7
Output : 4
Explanation :
After applying the color 1 to subtree at node 3, node 7 contains color 1.
After applying the color 2 to subtree at node 2, node 7 contains color 1.
After applying the color 3 to subtree at node 1, node 7 contains color 3.
After applying the color 4 to subtree at node 3, node 7 contains color 4.

Input : arr[] = {3, 2, 1, 3}, K = 4
Output : 3
Explanation :
After applying the color 1 to subtree at node 3, node 4 contains color 0.
After applying the color 2 to subtree at node 2, node 4 contains color 2.
After applying the color 3 to subtree at node 1, node 4 contains color 3.
After applying the color 4 to subtree at node 3, node 4 contains color 3.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A naive approach is for every index i (1 <= i <= N) update color all nodes in a subtree until the level of the nodes less than the level of given node K. After N such operations print the color of node K.

An efficient approach is to use hashing

• First, insert all the nodes with their respective colors in a map( in case, if a node has more than one color, keep one with the maximum color number).
• Find all ancestors of the given node k and output the color having a maximum number. (If the current node is x, then (x-1)/2 or x/2 are its ancestors)

Below is the implementation of the above approach:

## CPP

 `// CPP program to find color of the node ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find color of the node ` `int` `findColor(map<``int``, ``int``> mapWithColor, ``int` `query) ` `{ ` `    ``// Maximum is to store maximum color ` `    ``int` `maximum = 0; ` ` `  `    ``// Loop to check all the parent  ` `    ``// values to get maximum color ` `    ``while` `(query >= 1) { ` ` `  `        ``// Find the number into map and get maximum color ` `        ``if` `(mapWithColor.find(query) != mapWithColor.end()) ` `        ``{ ` `            ``// Take the maximum color and ` `            ``// assign into maximum variable ` `            ``maximum = max(maximum, mapWithColor[query]); ` `        ``} ` ` `  `        ``// Find parent index ` `        ``if` `(query % 2 == 1) ` `            ``query = (query - 1) / 2; ` `        ``else` `            ``query = query / 2; ` `    ``} ` ` `  `    ``// Return maximum color ` `    ``return` `maximum; ` `} ` ` `  `// Function to build hash map with color ` `map<``int``, ``int``> buildMapWithColor(``int` `arr[], ``int` `n) ` `{ ` `    ``// To store color of each node ` `    ``map<``int``, ``int``> mapWithColor; ` ` `  `    ``// For each number add a color number ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// Assigning color ` `        ``mapWithColor[arr[i]] = i + 1; ` `    ``} ` ` `  `    ``// Return hash map ` `    ``return` `mapWithColor; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 2, 1, 3 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``int` `k = 7; ` ` `  `    ``// Build mapWithColor ` `    ``map<``int``, ``int``> mapWithColor = buildMapWithColor(arr, n); ` ` `  `    ``// Print the maximum color ` `    ``cout << findColor(mapWithColor, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// JAVA program to find color of the node ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find color of the node ` `    ``static` `int` `findColor(HashMap mapWithColor, ``int` `query) ` `    ``{ ` `        ``// Maximum is to store maximum color ` `        ``int` `maximum = ``0``; ` ` `  `        ``// Loop to check all the parent ` `        ``// values to get maximum color ` `        ``while` `(query >= ``1``) ` `        ``{ ` ` `  `            ``// Find the number into map and get maximum color ` `            ``if` `(mapWithColor.containsKey(query))  ` `            ``{ ` `                ``// Take the maximum color and ` `                ``// assign into maximum variable ` `                ``maximum = Math.max(maximum, mapWithColor.get(query)); ` `            ``} ` ` `  `            ``// Find parent index ` `            ``if` `(query % ``2` `== ``1``) ` `                ``query = (query - ``1``) / ``2``; ` `            ``else` `                ``query = query / ``2``; ` `        ``} ` ` `  `        ``// Return maximum color ` `        ``return` `maximum; ` `    ``} ` ` `  `    ``// Function to build hash map with color ` `    ``static` `HashMap buildMapWithColor(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``// To store color of each node ` `        ``HashMap mapWithColor = ``new` `HashMap(); ` ` `  `        ``// For each number add a color number ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``// Assigning color ` `            ``mapWithColor.put(arr[i], i + ``1``); ` `        ``} ` ` `  `        ``// Return hash map ` `        ``return` `mapWithColor; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``3``, ``2``, ``1``, ``3` `}; ` ` `  `        ``int` `n = arr.length; ` ` `  `        ``int` `k = ``7``; ` ` `  `        ``// Build mapWithColor ` `        ``HashMap mapWithColor = buildMapWithColor(arr, n); ` ` `  `        ``// Print the maximum color ` `        ``System.out.print(findColor(mapWithColor, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 code program to find color of the node ` ` `  `# Function to find color of the node ` `def` `findColor(mapWithColor, query): ` ` `  `    ``# Maximum is to store maximum color ` `    ``maximum ``=` `0` ` `  `    ``# Loop to check all the parent ` `    ``# values to get maximum color ` `    ``while` `(query >``=` `1``): ` ` `  `        ``# Find the number into map and get maximum color ` `        ``if` `(query) ``in` `mapWithColor.keys(): ` `            ``# Take the maximum color and ` `            ``# assign into maximum variable ` `            ``maximum ``=` `max``(maximum, mapWithColor[query]) ` ` `  `        ``# Find parent index ` `        ``if` `(query ``%` `2` `=``=` `1``): ` `            ``query ``=` `(query ``-` `1``) ``/``/` `2` `        ``else``: ` `            ``query ``=` `query ``/``/` `2` ` `  ` `  `    ``# Return maximum color ` `    ``return` `maximum ` ` `  `# Function to build hash map with color ` `def` `buildMapWithColor(arr, n): ` `    ``# To store color of each node ` `    ``mapWithColor``=``{} ` ` `  `    ``# For each number add a color number ` `    ``for` `i ``in` `range``(n): ` `        ``# Assigning color ` `        ``mapWithColor[arr[i]] ``=` `i ``+` `1` ` `  `    ``# Return hash map ` `    ``return` `mapWithColor ` ` `  ` `  `# Driver code ` ` `  `arr ``=` `[``3``, ``2``, ``1``, ``3``] ` ` `  `n ``=` `len``(arr) ` ` `  `k ``=` `7` ` `  `# Build mapWithColor ` `mapWithColor ``=` `buildMapWithColor(arr, n) ` ` `  `# Print the maximum color ` `print``(findColor(mapWithColor, k)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# program to find color of the node ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find color of the node ` `    ``static` `int` `findColor(Dictionary<``int``, ``int``> mapWithColor, ``int` `query) ` `    ``{ ` `        ``// Maximum is to store maximum color ` `        ``int` `maximum = 0; ` ` `  `        ``// Loop to check all the parent ` `        ``// values to get maximum color ` `        ``while` `(query >= 1) ` `        ``{ ` ` `  `            ``// Find the number into map and get maximum color ` `            ``if` `(mapWithColor.ContainsKey(query))  ` `            ``{ ` `                ``// Take the maximum color and ` `                ``// assign into maximum variable ` `                ``maximum = Math.Max(maximum, mapWithColor[query]); ` `            ``} ` ` `  `            ``// Find parent index ` `            ``if` `(query % 2 == 1) ` `                ``query = (query - 1) / 2; ` `            ``else` `                ``query = query / 2; ` `        ``} ` ` `  `        ``// Return maximum color ` `        ``return` `maximum; ` `    ``} ` ` `  `    ``// Function to build hash map with color ` `    ``static` `Dictionary<``int``, ``int``> buildMapWithColor(``int` `[]arr, ``int` `n)  ` `    ``{ ` `        ``// To store color of each node ` `        ``Dictionary<``int``, ``int``> mapWithColor = ``new` `Dictionary<``int``, ``int``>(); ` ` `  `        ``// For each number add a color number ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``// Assigning color ` `            ``if``(mapWithColor.ContainsKey(arr[i])) ` `                ``mapWithColor[arr[i]] = i + 1; ` `            ``else` `                ``mapWithColor.Add(arr[i], i + 1); ` `        ``} ` ` `  `        ``// Return hash map ` `        ``return` `mapWithColor; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int` `[]arr = { 3, 2, 1, 3 }; ` ` `  `        ``int` `n = arr.Length; ` ` `  `        ``int` `k = 7; ` ` `  `        ``// Build mapWithColor ` `        ``Dictionary<``int``, ``int``> mapWithColor = buildMapWithColor(arr, n); ` ` `  `        ``// Print the maximum color ` `        ``Console.Write(findColor(mapWithColor, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```4
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.