Given a weighted tree containing N nodes, and two nodes u and v, the task is to find the count of nodes having prime weight on the simple path between u and v (both inclusive).
u = 3, v = 5
Prime weight on path 3 to 5 is [11, 5]. Hence the answer is 2.
Approach: To solve the problem mentioned above, the idea is to use the basic concept when we find the LCA of two nodes.
- Precompute all the prime numbers till MAX using the Sieve method to check if a number is prime or not in O(1)
- Given two nodes u and v, we will make both nodes at the same level, by moving the greater level node move upwards. As we move up we will also check if the weight is prime or not.
- If v == u then we will simply check the weight of the current node and return the count.
- If v is not equal to u then we will move both u and v upward by 1 till they are not the same.
- Now we will finally check the weight of the first ancestor of u or v and return the count.
Below is the implementation of the above approach:
Time Complexity: O(N).
In dfs, every node of the tree is processed once, and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Also, for processing each node the SieveOfEratosthenes() function is used which has a complexity of O(sqrt(N)) too but since this function is executed only once, it does not affect the overall time complexity. Therefore, the time complexity is O(N).
Auxiliary Space: O(N).
Extra space is used for the prime array, so the space complexity is O(N).
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.
- Count the nodes in the given tree whose weight is prime
- Count of Nodes which has Prime Digit sum weight in a Tree
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is a power of two
- Count the nodes in the given tree whose weight is even parity
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count the nodes of the given tree whose weight has X as a factor
- Count nodes in the given tree whose weight is a fibonacci number
- Count the nodes in the given tree whose weight is a powerful number
- Count the nodes in the given Tree whose weight is a Perfect Number
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Product of minimum edge weight between all pairs of a Tree
- Count the nodes whose weight is a perfect square
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a given range whose count of prime factors is a Prime Number
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Number of Paths of Weight W in a K-ary tree
- Count of nodes in a Binary Tree whose child is its prime factors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.