# Kth largest node among all directly connected nodes to the given node in an undirected graph

Given two arrays **u** and **v**, representing a graph such that there is an undirected edge from **u[i]** to **v[i]** (0 ≤ v[i], u[i] < N) and each node has some value **val[i]** (0 ≤ i < N). For each node, if the nodes connected directly to it are sorted in descending order according to their values (in case of equal values, sort it according to their indices in ascending order), print the number of the node at **k ^{th}** position. If total nodes are

**< k**then print

**-1**.

**Examples:**

Input:u[] = {0, 0, 1}, v[] = {2, 1, 2}, val[] = {2, 4, 3}, k = 2

Output:

2

0

0

For 0 node, the nodes directly connected to it are 1 and 2

having values 4 and 3 respectively, thus node with 2nd largest value is 2.

For 1 node, the nodes directly connected to it are 0 and 2

having values 2 and 3 respectively, thus node with 2nd largest value is 0.

For 2 node, the nodes directly connected to it are 0 and 1

having values 2 and 4 respectively, thus node with 2nd largest value is 0.

Input:u[] = {0, 2}, v[] = {2, 1}, val[] = {2, 4, 3}, k = 2

Output:

-1

-1

0

**Approach:** The idea is to store the nodes directly connected to a node along with their values in a vector and sort them in increasing order, and the kth largest value for a node, having **n** number of nodes directly connected to it, will be **(n – k)**th node from last.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print Kth node for each node ` `void` `findKthNode(` `int` `u[], ` `int` `v[], ` `int` `n, ` `int` `val[], ` `int` `V, ` `int` `k) ` `{ ` ` ` ` ` `// Vector to store nodes directly ` ` ` `// connected to ith node along with ` ` ` `// their values ` ` ` `vector<pair<` `int` `, ` `int` `> > g[V]; ` ` ` ` ` `// Add edges to the vector along with ` ` ` `// the values of the node ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `g[u[i]].push_back(make_pair(val[v[i]], v[i])); ` ` ` `g[v[i]].push_back(make_pair(val[u[i]], u[i])); ` ` ` `} ` ` ` ` ` `// Sort neighbors of every node ` ` ` `// and find the Kth node ` ` ` `for` `(` `int` `i = 0; i < V; i++) { ` ` ` `if` `(g[i].size() > 0) ` ` ` `sort(g[i].begin(), g[i].end()); ` ` ` ` ` `// Get the kth node ` ` ` `if` `(k <= g[i].size()) ` ` ` `printf` `(` `"%d\n"` `, g[i][g[i].size() - k].second); ` ` ` ` ` `// If total nodes are < k ` ` ` `else` ` ` `printf` `(` `"-1\n"` `); ` ` ` `} ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `V = 3; ` ` ` `int` `val[] = { 2, 4, 3 }; ` ` ` `int` `u[] = { 0, 0, 1 }; ` ` ` `int` `v[] = { 2, 1, 2 }; ` ` ` ` ` `int` `n = ` `sizeof` `(u) / ` `sizeof` `(` `int` `); ` ` ` `int` `k = 2; ` ` ` ` ` `findKthNode(u, v, n, val, V, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// pair class ` `static` `class` `pair ` `{ ` ` ` `int` `first,second; ` ` ` `pair(` `int` `a,` `int` `b) ` ` ` `{ ` ` ` `first = a; ` ` ` `second = b; ` ` ` `} ` `} ` ` ` `// Function to print Kth node for each node ` `static` `void` `findKthNode(` `int` `u[], ` `int` `v[], ` `int` `n, ` ` ` `int` `val[], ` `int` `V, ` `int` `k) ` `{ ` ` ` ` ` `// Vector to store nodes directly ` ` ` `// connected to ith node along with ` ` ` `// their values ` ` ` `Vector<Vector<pair >> g = ` `new` `Vector<Vector<pair>>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < V; i++) ` ` ` `g.add(` `new` `Vector<pair>()); ` ` ` ` ` `// Add edges to the Vector along with ` ` ` `// the values of the node ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `g.get(u[i]).add(` `new` `pair(val[v[i]], v[i])); ` ` ` `g.get(v[i]).add(` `new` `pair(val[u[i]], u[i])); ` ` ` `} ` ` ` ` ` `// Sort neighbors of every node ` ` ` `// and find the Kth node ` ` ` `for` `(` `int` `i = ` `0` `; i < V; i++) ` ` ` `{ ` ` ` `if` `(g.get(i).size() > ` `0` `) ` ` ` `Collections.sort(g.get(i),` `new` `Comparator<pair>() ` ` ` `{ ` ` ` `public` `int` `compare(pair p1, pair p2) ` ` ` `{ ` ` ` `return` `p1.first - p2.first; ` ` ` `} ` ` ` `}); ` ` ` ` ` `// Get the kth node ` ` ` `if` `(k <= g.get(i).size()) ` ` ` `System.out.printf(` `"%d\n"` `, g.get(i).get(g.get(i).size() - k).second); ` ` ` ` ` `// If total nodes are < k ` ` ` `else` ` ` `System.out.printf(` `"-1\n"` `); ` ` ` `} ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `V = ` `3` `; ` ` ` `int` `val[] = { ` `2` `, ` `4` `, ` `3` `}; ` ` ` `int` `u[] = { ` `0` `, ` `0` `, ` `1` `}; ` ` ` `int` `v[] = { ` `2` `, ` `1` `, ` `2` `}; ` ` ` ` ` `int` `n = u.length; ` ` ` `int` `k = ` `2` `; ` ` ` ` ` `findKthNode(u, v, n, val, V, k); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to print Kth node for each node ` `def` `findKthNode(u, v, n, val, V, k): ` ` ` ` ` `# Vector to store nodes directly connected ` ` ` `# to ith node along with their values ` ` ` `g ` `=` `[[] ` `for` `i ` `in` `range` `(V)] ` ` ` ` ` `# Add edges to the vector along ` ` ` `# with the values of the node ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `g[u[i]].append((val[v[i]], v[i])) ` ` ` `g[v[i]].append((val[u[i]], u[i])) ` ` ` ` ` `# Sort neighbors of every node ` ` ` `# and find the Kth node ` ` ` `for` `i ` `in` `range` `(` `0` `, V): ` ` ` `if` `len` `(g[i]) > ` `0` `: ` ` ` `g[i].sort() ` ` ` ` ` `# Get the kth node ` ` ` `if` `k <` `=` `len` `(g[i]): ` ` ` `print` `(g[i][` `-` `k][` `1` `]) ` ` ` ` ` `# If total nodes are < k ` ` ` `else` `: ` ` ` `print` `(` `"-1"` `) ` ` ` ` ` `return` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `V ` `=` `3` ` ` `val ` `=` `[` `2` `, ` `4` `, ` `3` `] ` ` ` `u ` `=` `[` `0` `, ` `0` `, ` `1` `] ` ` ` `v ` `=` `[` `2` `, ` `1` `, ` `2` `] ` ` ` `n, k ` `=` `len` `(u), ` `2` ` ` ` ` `findKthNode(u, v, n, val, V, k) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

2 0 0

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Largest subarray sum of all connected components in undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Convert undirected connected graph to strongly connected directed graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Connected Components in an undirected graph
- Cycles of length n in an undirected and connected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Clone an undirected graph with multiple connected components
- Count of unique lengths of connected components for an undirected graph using STL
- Program to count Number of connected components in an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Check if longest connected component forms a palindrome in undirected graph
- Maximum number of nodes which can be reached from each node in a graph.
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Sum of degrees of all nodes of a undirected graph
- Nodes with prime degree in an undirected Graph
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Number of ways to select a node from each connected component
- Minimum cost path from source node to destination node via an intermediate node

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.