Dsa
Trees
Medium
The Peak Trail
A 'Mountain Path' in a tree of integers is a simple path that strictly increases to a peak node and then strictly decreases. A path must have at least one increasing step and at least one decreasing step (length >= 2, with a clear peak). Find the number of nodes in the longest Mountain Path (the count of nodes, not edges).
Input Format:
- Line 1: N, the number of nodes.
- Line 2: N integers, the value at each node (1-indexed).
- Next N-1 lines: u v (edge between u and v).
Output Format:
- An integer representing the maximum nodes in a Mountain Path. If no such path exists, output 0.
Example:
Input:
5
1 3 5 3 1
1 2
2 3
3 4
4 5
Output:
5
(Path 1-2-3-4-5 has values 1, 3, 5, 3, 1. It increases then decreases.)
Key concepts
tree dplongest path
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