This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
Task #2 this week is described as follows:
You are given a list, @L
, of three or more random integers between 1 and 50. A Noble Integer is an integer N in @L
, such that there are exactly N integers greater than N in @L
. Output any Noble Integer found in @L
, or an empty list if none were found.
An interesting question is whether or not there can be multiple Noble Integers in a list.
For example, suppose we have list of 4 integers [2, 6, 1, 3].
Here we have 2 in the above list, known as Noble Integer, since there are exactly 2 integers in the list i.e. 3 and 6, which are greater than 2.
Therefore the script would print 2.
While Mohammad gave me credit for submitting this problem, I only suggested some wording changes right before it was published, so I didn’t have any sort of advantage going in.
The algorithm I came up with for finding Noble Integers is fairly simple and seems obvious: simply sort the array, and then for each array index, $i
, @L.end - $i
is the number of elements that come after. @L.end
in Raku is $#L
in Perl: the last index in the array.