Special Paths | Google OA | July 2022 | Graph
Anonymous User
7666
Jul 09, 2022
Jul 11, 2022
You are given the following:
- A tree with N nodes
- An array A denoting the value of each node
A path is called special path if the following conditions are satisfied:
- All nodes in the path are traversed exactly once.
- The value of starting and terminating node in the path is same and startnode != terminating node
- The values of any node in the path are not greater than the value of starting node.
Task:
Count the no. of special paths in the tree. (Two paths are different if they contain at least one different node.)
Example:
N = 5
edges = [(1,2), (1,3), (3,4), (3,5)]
A = [2,3,1,2,3]

Output:
2
Explanation:
path 1: 1(2)->3(1)->4(2)
path 2: 2(3)->1(2)->3(1)->5(3)
Constraints
1<= T <= 10
2 <= N <= 10^5
1<=Ai <= 10^9
I tried brute force by applying DFS at every node. I got TLE.
Any suggestions or approach is appreciated :)
Comments (12)


