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