Dsa
Trees
Medium
The Prime Orchard
In a forest represented by a tree, some nodes are 'Prime Trees' (their value is a prime number). Find the length of the longest path (number of nodes) where every node on the path is a Prime Tree. Note: 1 is not prime.
Input Format:
- Line 1: N.
- Line 2: N integers (values of nodes 0 to N-1).
- Next N-1 lines: u v (edges).
Output Format:
- An integer representing the number of nodes in the longest prime-only path.
Example:
Input:
5
2 3 5 4 7
0 1
1 2
2 3
0 4
Output:
4
Explanation: Primes are 2, 3, 5, 7. Path 5-3-2-7 has 4 nodes. Node 4 (value 4) is not prime.
Key concepts
treediameternumber theory
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now