#K5121. Shortest Subarray with Maximum Equal to k
Shortest Subarray with Maximum Equal to k
Shortest Subarray with Maximum Equal to k
Given an integer ( k ) and an array of ( n ) integers, you are to determine the length of the shortest contiguous subarray whose maximum value is exactly ( k ). In other words, find a continuous segment of the array such that ( \max(subarray) = k ). If there exists at least one element in the array equal to ( k ), then the answer is ( 1 ) (since a single element subarray ( [k] ) satisfies the condition). Otherwise, output ( -1 ).
Note: The input consists of multiple test cases. For each test case, if there is an occurrence of ( k ) in the given array then the answer is ( 1 ); if ( k ) does not appear in the array, output ( -1 ).
inputFormat
The first line of input contains an integer ( T ) representing the number of test cases. Each test case consists of two lines:
1. The first line contains two integers ( n ) and ( k ) where ( n ) is the number of elements in the array and ( k ) is the integer to compare against.
2. The second line contains ( n ) space-separated integers representing the array elements.
outputFormat
For each test case, output a single line containing one integer: the length of the shortest contiguous subarray whose maximum value is exactly ( k ). If no such subarray exists, output ( -1 ).## sample
4
5 4
1 2 4 3 5
3 10
3 10 2
6 7
7 2 3 1 7 8
4 1
1 1 1 1
1
1
1
1
</p>