#C1193. Maximum Element Not Exceeding K
Maximum Element Not Exceeding K
Maximum Element Not Exceeding K
Given an array of integers and an integer K, your task is to find the maximum element in the array that is less than or equal to K. Formally, you need to compute:
$$\max\{a_i \mid a_i \leq K\}$$
If no such element exists, output -1
.
You will be given T test cases. For each test case, the first line contains two integers N and K, where N is the size of the array. The next line contains N space-separated integers. You are to process each test case and print the result on a new line.
Example:
Input: 3 5 10 1 2 3 4 5 4 3 10 20 30 40 3 15 5 15 25</p>Output: 5 -1 15
inputFormat
The first line of input contains a single integer T representing the number of test cases.
For each test case, the first line contains two integers N and K where:
- N is the number of elements in the array.
- K is the target value.
The second line contains N space-separated integers representing the array elements.
outputFormat
For each test case, output a single integer on a new line: the maximum element in the array that is less than or equal to K. If no such element exists, output -1
.
3
5 10
1 2 3 4 5
4 3
10 20 30 40
3 15
5 15 25
5
-1
15
</p>