20
1
This problem is "inspired" from a question that was originally asked on Quora (not for code golfing). I just want to make it a challenge for you guys (and my first problem submission here).
Given an array of integer elements v
and an integer d
(we assume that d is lower or equal to the array's length), consider all the sequences of d
consecutive elements in the array. For each sequence, compute the difference between the maximum and minimum value of the elements in that sequence and name it the deviation.
Your task is to write a program or function that computes the maximum value among all deviations of all the sequences considered above, and return or output that value.
Worked-through Example:
v: (6,9,4,7,4,1)
d: 3
The sequences of length 3 are:
6,9,4 with deviation 5
9,4,7 with deviation 5
4,7,4 with deviation 3
7,4,1 with deviation 6
Thus the maximal deviation is 6, so the output is 6.
This is code golf, so the shortest answer in bytes wins.
+1 for TIL that you can use
with
on an entire lambda function – Bassdrop Cumberwubwubwub – 2016-10-21T15:18:30.860Actually,
Uncaught SyntaxError: Unexpected token with
. Can you post a working snippet? – Bassdrop Cumberwubwubwub – 2016-10-21T15:21:04.843@BassdropCumberwubwubwub If you want to name the lambda you need to put the assignment after the
with(Math)
, or usef=eval("with(Math)(v,d)=>max(...a)))")
. – Neil – 2016-10-21T15:43:04.723