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