Given a sorted array and a number key, find the smallest array element which is greater than the key.
If the key is greater than or equal to the largest element then return the key itself.
Expected Time Complexity: O(log n)
Array: [1, 2, 3, 3, 4, 4, 8, 10]
Number: 4
Answer: 8
Array: [1, 2, 3, 3, 3, 4, 4, 5]
Number: 5
Answer: 5 (Largest Element)
Array: [1, 2, 3, 3, 3, 4, 4, 5]
Number: -5
Answer: 1
First-line contains an integer âTâ denoting the number of test cases.
For each test case the input has two lines:
T lines each contain the next greater element for each test case.
2
8 4
1 2 3 3 4 4 8 10
8 5
1 2 3 3 3 4 4 5
8
5
1 <= T <= 100
1 <= n <= 104
-106 <= Ai <= 106
-106 <= key <= 106