Kth largest node among all directly connected nodes to the given node in an undirected graph | Function to print Kth node for each node ; Vector to store nodes directly connected to ith node along with their values ; Add edges to the vector along with the values of the node ; Sort neighbors of every node and find the Kth node ; Get the kth node ; If total nodes are < k ; Driver code
function findKthNode ( u , v , n , val , V , k ) { var g = Array . from ( Array ( V ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) { g [ u [ i ] ] . push ( [ val [ v [ i ] ] , v [ i ] ] ) ; g [ v [ i ] ] . push ( [ val [ u [ i ] ] , u [ i ] ] ) ; } for ( var i = 0 ; i < V ; i ++ ) { if ( g [ i ] . length > 0 ) g [ i ] . sort ( ) ; if ( k <= g [ i ] . length ) document . write ( g [ i ] [ g [ i ] . length - k ] [ 1 ] + " " ) ; else document . write ( " " ) ; } return ; } var V = 3 ; var val = [ 2 , 4 , 3 ] ; var u = [ 0 , 0 , 1 ] ; var v = [ 2 , 1 , 2 ] ; var n = u . length ; var k = 2 ; findKthNode ( u , v , n , val , V , k ) ;