# Find the longest run of consecutive digits

Simple challenge: given a series of positive integer numbers, find the number that contains among its digits the longest run of consecutive digits. The trick? It's allowed for the digits in the runs to wrap around the possible values (0123456789) and to run backwards. So both 2345, 89012 and 5432109 are valid runs of consecutive digits (but not 3456765 nor 321090123 as the run must be always in the same direction, although 3456765 can be considered as two runs: 34567 and 765). In the case of ties, return the first one.

Test cases:

Input:  [3274569283, 387652323, 23987654323648, 2345687913624]
Output: 23987654323648
(The run is 98765432; run length: 8)

Input:  [123012363672023, 098761766325432, 15890123456765]
Output: 15890123456765
(The run is 8901234567; run length: 10)

Input:  [43, 19, 456]
Output: 456

Input:  [5, 9, 0]
Output: 5

Input:  [71232107, 7012347]
Output: 7012347

Input:  [1234, 32109876]
Output: 32109876

Input:  [9090, 123]
Output: 123


Notes:

• There will be at least one number in the input.
• Input numbers can contain leading zeroes.
• Input and output can be in any reasonable format. So input numbers can be taken as strings, lists of digits/characters...
• Output can contain trailing and/or leading whitespaces and newlines as long as the number is printed.
• This is , so may the shortest program/function for each language win!
• Aug 2 '17 at 12:05
• Just to be sure, the list itself cannot wrap, right? (I misunderstood the digit wrapping as list wrapping) so [7,8,1,6] has a maximal run of [7,8] rather than [6,7,8], yes? Aug 2 '17 at 16:23
• @JonathanAllan yes, the maximal run is 78 in that case. Aug 2 '17 at 16:43

# Jelly, 18 bytes

I9,-;N¤yŒgỊS€ṀµÐṀḢ


Try it online!

Takes and returns as list of digits so as to preserve leading zeroes.

• Similar problem to mine - try this out for size (I believe it returns the wrong result - I've suggested it as a test case just to be sure). Aug 2 '17 at 13:25
• @JonathanAllan I think that's the right output? (there's 3210 in the first number btw) Aug 2 '17 at 13:27
• Ah oops, this, sorry! Aug 2 '17 at 13:30
• @JonathanAllan Oh I see what you mean...it's probably because of the A in there. Aug 2 '17 at 13:32
• @JonathanAllan Fixed. Aug 2 '17 at 17:46

## JavaScript (ES6), 104102 98 bytes

Takes input as a list of lists of digits. Returns the best one.

a=>a.map(s=>s.map(n=>(i=(d=(x-(x=n)+11)%10)&&d-2?0:d-p?(p=d,1):i+1)>j&&(r=s,j=i),p=x=-10),j=-1)&&r


### Test cases

let f =

a=>a.map(s=>s.map(n=>(i=(d=(x-(x=n)+11)%10)&&d-2?0:d-p?(p=d,1):i+1)>j&&(r=s,j=i),p=x=-10),j=-1)&&r

console.log(JSON.stringify(f([[3,2,7,4,5,6,9,2,8,3], [3,8,7,6,5,2,3,2,3], [2,3,9,8,7,6,5,4,3,2,3,6,4,8], [2,3,4,5,6,8,7,9,1,3,6,2,4]])))
console.log(JSON.stringify(f([[1,2,3,0,1,2,3,6,3,6,7,2,0,2,3], [0,9,8,7,6,1,7,6,6,3,2,5,4,3,2], [1,5,8,9,0,1,2,3,4,5,6,7,6,5]])))
console.log(JSON.stringify(f([[4,3], [1,9], [4,5,6]])))
console.log(JSON.stringify(f([, , ])))

# Jelly,  18 16  15 bytes

I%⁵Œg%8ċ€1ṀµÐṀḢ


A monadic link taking a list of lists of digits, and returning the leftmost one containing a maximal run as described.

Try it online! or see a test suite (with processing to make I/O look like it is in the question).

### How?

I%⁵Œg%8ċ€1ṀµÐṀḢ - Link: list of lists of integers (digits) from [0-9]
µÐṀ  - keep elements for which the link to the left is maximal:
I               -   incremental differences (i.e. [a2-a1, a3-a2, ...])
⁵             -   literal 10
%              -   modulo by (i.e. [(a2-a1)%10, (a3-a2)%10, ...])
-     this equates deltas of -9 and -1 with 1 and 9 respectively
Œg           -   group runs of equal elements
%8         -   modulo by 8; vectorised (9s become 1s, others unaffected)
ċ€1      -   count number of 1s in €ach group
Ṁ     -   maximum
Ḣ - head (get the first one of those that were maximal)

• V€ not sure about that, you may have to count leading zeroes. Aug 2 '17 at 12:25
• That does count leading zeros of the sting input, however I see we may take lists of lists of digits... Aug 2 '17 at 12:26
• I think you're supposed to support leading zeroes. Aug 2 '17 at 12:28
• I do support leading zeros Aug 2 '17 at 12:29
• I read that as "That doesn't count..." Aug 2 '17 at 12:29

# Python 2, 118 bytes

Takes a list of lists of digits a; returns one of its lists.

lambda a:max(a,key=lambda l:len(max(re.findall('1+|9*',[(x-y)%10for x,y in zip(l,l[1:])][1::3]),key=len)))
import re


Try it online!

• Fails on input [[9,0,9,0],[1,2,3]]. Aug 2 '17 at 15:03
• @Zgarb Oops, you’re right. Back to an old version I go.
– Lynn
Aug 2 '17 at 15:22

## Husk, 20 bytes

←Ö¤>(→Of€1†%8gẊo%10-


Takes and returns a list of lists of digits. Try it online!

## Explanation

←Ö¤>(→Of€1†%8gẊo%10-  Implicit input.
←                     Return first element of
Ö                    the input sorted in a stable manner
>                  in descending order
¤ (                 with respect to the following function:
Argument is list of digits, say [5,2,1,0,9,1,0].
-   Differences
o%10    mod 10
Ẋ        of all adjacent pairs: [7,9,9,9,2,1]
g         Group adjacent equal elements: [,[9,9,9],,]
†%8          Vectorized mod 8: [,[1,1,1],,]
f€1             Keep those runs where 1 occurs: [[1,1,1],]
O                Sort in ascending order: [,[1,1,1]]
→                 Take last element (gives [] on empty list): [1,1,1]
This is a list of 1s with length one less than
the longest run of consecutive digits.

• Doesn't seem to give the right answer for multiple test cases. Oct 14 '20 at 10:09
• @Razetime Fixed. I'm not sure, but I suspect that either Ö or < has been changed since this challenge, since the fix was so simple. Oct 14 '20 at 11:25

# MATLAB, 130 bytes

Take input to array, array of column differences [X(2)-X(1),...,X(n)-X(n-1)], check the most frequent value in the array (1 ascending order -1 otherwise), get the index for either the most frequent value or -9 multiplied by the most frequent value (-9 occurs in ascending order, 9 otherwise), find the consecutive indices (i.e. whose difference is equal to 1) and sum it please, because it's late. Output the largest.

a=input('')
t=[]
for i=1:numel(a)
b=diff(num2str(a(i))-'0')
c=mode(b)
t=[t sum(diff(find(b==c|b==-9*c))==1)]
end
[t,I]=max(t),a(I)


Try it online!