#K81967. Longest Tower Coverage Subarray
Longest Tower Coverage Subarray
Longest Tower Coverage Subarray
You are given an array of integers representing the heights of buildings and a tower of height (h). Your task is to determine the length of the longest consecutive subarray (contiguous segment) such that every building in the segment has a height not exceeding (h), and there is at least one building in that segment with height exactly equal to (h).
More formally, given an array (a_1, a_2, \dots, a_n), find the maximum length (L) such that there exists some index range ([i, j]) (with (j-i+1=L)) satisfying:\
- For every (k) in ([i, j]), (a_k \leq h).\
- There exists at least one (k) in ([i, j]) with (a_k = h).
If no such subarray exists, output 0.
inputFormat
The first line of input contains two integers (n) and (h), where (n) is the number of buildings, and (h) is the height of the tower. The second line contains (n) space-separated integers representing the heights of the buildings.
outputFormat
Output a single integer, which is the length of the longest contiguous subarray that satisfies the conditions. If there is no valid subarray, print 0.## sample
7 4
3 1 4 1 5 9 2
3