Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Find the largest number n positions away from n

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 a 7 located 7 positions to its right
  • The first 6, as there is a 3 located 3 positions to its right
  • The 3, as there is a 4 located 4 positions to its left
  • The 5, as there is a 2 located 2 positions to its right
  • The second 7, as there is a 3 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

Answer*

Draft saved
Draft discarded
Cancel
5
  • \$\begingroup\$ Input format is flexible by default. You can take v as a column vector, just state that in the answer. Also, you replace find by ~~ to save two more bytes \$\endgroup\$ Commented Sep 22, 2016 at 10:47
  • \$\begingroup\$ @LuisMendo Thanks, I edited the post to incorporate your suggestions! \$\endgroup\$ Commented Sep 22, 2016 at 11:04
  • \$\begingroup\$ For different languages (or a significantly different approach in the same language) you should post another answer. There's a MATL chatroom should you have any questions regarding the language \$\endgroup\$ Commented Sep 22, 2016 at 12:12
  • \$\begingroup\$ BTW, due to a bug in MATL's toeplitz (YT), it uses two inputs (not one) by default \$\endgroup\$ Commented Sep 22, 2016 at 12:13
  • \$\begingroup\$ Ok cool. I translated it to MATL and posted another answer here: codegolf.stackexchange.com/a/94183/42247 \$\endgroup\$ Commented Sep 22, 2016 at 13:34

AltStyle によって変換されたページ (->オリジナル) /