#C6863. Max Friendly Trees
Max Friendly Trees
Max Friendly Trees
You are given a forest of trees where each tree has a friendship level. The objective is to form a friendly group by selecting as many trees as possible such that the difference between the minimum and maximum friendship levels in the group is at most \(F\). In other words, if you choose any group of trees and let \(m\) be the minimum and \(M\) be the maximum friendship level among them, then the group is friendly if \(M - m \leq F\).
To solve the problem, you should first sort the friendship levels, and then find the largest contiguous subsequence that satisfies \(\text{max} - \text{min} \leq F\). Your task is to determine the maximum number of trees that can form such a group.
inputFormat
The input consists of three lines:
- The first line contains an integer \(n\) (1 ≤ \(n\) ≤ 105), the number of trees.
- The second line contains \(n\) space-separated integers representing the friendship levels of the trees.
- The third line contains an integer \(F\) (0 ≤ \(F\) ≤ 109), the maximum allowed absolute difference.
outputFormat
Output a single integer representing the maximum number of trees that can form a friendly group.
## sample5
2 1 3 8 7
2
3