Find the color of given node in an infinite binary tree | 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
function findColor ( mapWithColor , query ) { let maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . has ( query ) ) { maximum = Math . max ( maximum , mapWithColor . get ( query ) ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } function buildMapWithColor ( arr , n ) { let mapWithColor = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { mapWithColor . set ( arr [ i ] , i + 1 ) ; } return mapWithColor ; } let arr = [ 3 , 2 , 1 , 3 ] ; let n = arr . length ; let k = 7 ; let mapWithColor = buildMapWithColor ( arr , n ) ; document . write ( findColor ( mapWithColor , k ) ) ;