任务
给定任何整数数组,例如:
[-1,476,578,27,0,1,-1,1,2]
和该数组的索引(此示例使用基于0的索引,尽管您也可以使用基于1的索引。):
index = 5
v
[-1,476,578,27,0,1,-1,1,2]
然后返回大于该索引处元素的最接近数字。在该示例中,大于1的最接近数字是27(相距2个索引)。
index = 5
v
[-1,476,578,27,0,1,-1,1,2]
^
Nearest greater number
Output = 27
假设条件
- 最近的不包括包装。
- 永远不会给程序一个长度为1的数组(例如
[55]
)。 - 您将假定总有一个大于给定元素的数字。
- 如果等距离处的元素大2个数字,则可以返回任意一个。
I / O对
Input:
Index = 45
Array = [69, 43, 89, 93, 62, 25, 4, 11, 115, 87, 174, 60, 84, 58, 28, 67, 71, 157, 47, 8, 33, 192, 187, 87, 175, 32, 135, 25, 137, 92, 183, 151, 147, 7, 133, 7, 41, 12, 96, 147, 9, 134, 197, 3, 107, 164, 90, 199, 21, 71, 77, 62, 190, 122, 33, 127, 185, 58, 92, 106, 26, 24, 56, 79, 71, 24, 24, 114, 17, 84, 121, 188, 6, 177, 114, 159, 159, 102, 50, 136, 47, 32, 1, 199, 74, 141, 125, 23, 118, 9, 12, 100, 94, 166, 12, 9, 179, 147, 149, 178, 90, 71, 141, 49, 74, 100, 199, 160, 120, 14, 195, 112, 176, 164, 68, 88, 108, 72, 124, 173, 155, 146, 193, 30, 2, 186, 102, 45, 147, 99, 178, 84, 83, 93, 153, 11, 171, 186, 157, 32, 90, 57, 181, 5, 157, 106, 20, 5, 194, 130, 100, 97, 3, 87, 116, 57, 125, 157, 190, 83, 148, 90, 44, 156, 167, 131, 100, 58, 139, 183, 53, 91, 151, 65, 121, 61, 40, 80, 40, 68, 73, 20, 135, 197, 124, 190, 108, 66, 21, 27, 147, 118, 192, 29, 193, 27, 155, 93, 33, 129]
Output = 199
Input:
Index = 2
Array = [4,-2,1,-3,5]
Output = 4 OR 5
Input:
Index = 0
Array = [2124, -173, -155, 146, 193, -30, 2, 186, 102, 4545]
Output = 4545
Input:
Index = 0
Array = [1,0,2,3]
Output = 2
Input:
Index = 2
Array = [3,-1,-3,-2,5]
Output = -1 OR -2
2; [3,-1,-3,-2,5]
是一个不错的测试案例。有正数,但结果为负。
1; [7,1,-4,2]