29
1
A sequel to this question.
Task
Given an array of positive integers, find the largest element k for which:
There exists some positive integer distance n, so that the element in the array located n places to the left or right from k equals n.
The array is guaranteed to contain at least one element satisfying this condition.
The shortest code (in bytes) wins. You may choose whichever I/O format you like.
Example
Given the input
[4, 6, 7, 9, 3, 6, 5, 7, 2]
The eligible values are:
- The
4
, as there is a7
located 7 positions to its right - The first
6
, as there is a3
located 3 positions to its right - The
3
, as there is a4
located 4 positions to its left - The
5
, as there is a2
located 2 positions to its right - The second
7
, as there is a3
located 3 positions to its left.
Of these values, the largest is 7
.
Test cases
[1, 13] → 13
[2, 9, 8, 3, 72, 2] → 8
[5, 28, 14, 5, 6, 3, 4, 7] → 14
[1, 3, 5, 15, 4, 1, 2, 6, 7, 7] → 7
[5, 1, 3, 5, 2, 5, 5, 8, 5, 1, 5, 1, 2, 3] → 5
[5, 12, 2, 5, 4, 7, 3, 3, 6, 2, 10, 5, 5, 5, 4, 1, 8, 5] → 10
Two more (albeit slightly redundant) cases within the example: the first 6 (again) as there is a 5 five positions to it's right; or the second 7 (again) as there is a 6 six positions to it's left. – Jonathan Allan – 2016-09-21T19:19:42.233
>
@PeterTaylor "this" in "this element" refers to k. – Taemyr – 2016-09-22T08:13:42.390
1@Taemyr, that doesn't make sense for two reasons: firstly, because k is not stated to be an element; and secondly because we're asked to "find the largest element satisfying" the condition, so "this element" has an antecedent outside the condition. – Peter Taylor – 2016-09-22T08:28:36.493
@PeterTaylor Someone had edit the post incorrectly. It was right the first time :( – Lynn – 2016-09-22T09:17:40.177
2Maybe you could avoid all confusion by saying "find the largest element k such that" and then use k instead of this element in the definition? – Martin Ender – 2016-09-22T11:43:25.867
@MartinEnder I agree. That's much more understandable, and that's how I'd phrase it if I were writing it mathematically. – mbomb007 – 2016-09-22T16:05:12.987