| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Husk | 240912T202928Z | int 21h |
| 008 | Uiua | 231012T183704Z | chunes |
| 012 | Uiua | 231012T223333Z | Dominic |
| 019 | Haskell + hgl | 231001T013201Z | Wheat Wi |
| 114 | TSQL | 230911T054631Z | DanielOn |
| 074 | Rust | 230914T022237Z | SaNoy Sa |
| 022 | julia | 230911T051800Z | Damian P |
| nan | Scala 3 | 230911T015533Z | 138 Aspe |
| 032 | Perl 5 | 230912T100452Z | Kjetil S |
| 013 | Charcoal | 230911T142106Z | Neil |
| 061 | Google Sheets | 230912T052411Z | doubleun |
| 037 | R | 230910T183509Z | Dominic |
| 047 | Ruby | 230910T201656Z | G B |
| 093 | C clang 16 | 230911T142124Z | Peter Co |
| 051 | Python | 230910T182749Z | corvus_1 |
| 051 | JavaScript ES6 | 230910T185800Z | Arnauld |
| 510 | Nibbles | 230910T223254Z | Dominic |
| 065 | Excel | 230911T095557Z | Jos Wool |
| 006 | 05AB1E | 230911T065417Z | Kevin Cr |
| 051 | Factor + math.extras | 230911T051358Z | chunes |
| 022 | J | 230910T230259Z | Jonah |
| 007 | Nekomata | 230911T022103Z | alephalp |
| 4375 | Vyxal | 230910T230407Z | Jonathan |
| 005 | Jelly | 230910T222647Z | Jonathan |
| 045 | Python 3 | 230910T212648Z | xnor |
| 032 | Haskell | 230910T213207Z | xnor |
| 006 | Jelly | 230910T204725Z | ovs |
| 051 | Retina 0.8.2 | 230910T185910Z | Neil |
| 007 | Vyxal G | 230910T182623Z | MTN |
Husk, 10 bytes
→Σ►f=1gẊ-O
Takes in a list and outputs one number - how many consequent values are in there.
Explanation:
O # Sort the input list
Ẋ- # Subtract adjacent pairs
g # Group together equal adjacent differences
f=1 # Filter only the groups of 1s (the differences of the consequent numbers)
► # Take the longest sublist
Σ # Sum
→ # Increment by 1
Uiua, 8 bytes
/↥⊜⧻.⍘⊚⊝
Inspired by Dominic van Essen's Uiua answer.
/↥⊜⧻.⍘⊚⊝
⊝ # deduplicate
⍘⊚ # inverted where, i.e. how many times each element occurs
⊜⧻. # lengths of contiguous equal elements
/↥ # maximum
Original answer:
Uiua, 18 bytes
+1/↥⊜⧻.=1≡/-◫2⊝⊏⍏.
+1/↥⊜⧻.=1≡/-◫2⊝⊏⍏.
⊏⍏. # sort
⊝ # deduplicate
◫2 # windows of size two
≡/- # difference of each window
=1 # change non-ones to zeros
⊜⧻. # find length of each contiguous group of ones
/↥ # maximum
+1 # plus one
Uiua, 12 bytes
/↥⍘⊚-⇡⧻.⊝⊏⍏.
/↥⍘⊚-⇡⧻.⊝⊏⍏.
⊏⍏. # sort
⊝ # deduplicate
⇡⧻. # copy & make series of same length
- # subtract
⍘⊚ # invert where
/↥ # maximum
Haskell + hgl, 19 bytes
l<ixe(fl~<(<P1)<fe)
Uses xnor's method.
20 bytes
Here's one that works online:
P1<l<ih eq1<δ<sr<nb
Explanation
nbremoves all duplicates from the listsrsorts the listδgets the differences between consecutive elementsih eq1gets the longest contiguous sublist of1sltakes the lengthP1adds 1
Reflection
Some things can be improved:
- The glue in
fl~<(<P1)<feis too much, there should be some helper function that can reduce the amount of glue here. sr<nbcould probably be a single function.
T-SQL, 114 bytes
SELECT MAX(c)FROM(SELECT COUNT(b)c FROM(SELECT a*(a+1)/2-SUM(a)OVER(ORDER BY a)b FROM @t GROUP BY a)X GROUP BY b)Y
Readable version of the code:
DECLARE @t TABLE(i INT NOT NULL IDENTITY(1,1) --table with identity to emulate a 'list' (not counted in byte count)
, a INT NOT NULL);
INSERT INTO @t (a) --Assigning the 'list' values (not counted in byte count)
VALUES(100)
, (4)
, (200)
, (1)
, (3)
, (2);
SELECT MAX(c) --5. Determine the maximum count from the subquery
FROM(
SELECT COUNT(*)c --4. Count how many rows make up each group
FROM(
--After this query it is simply a matter of determining which value of b appears the most times, and how many times that is.
SELECT a*(a+1)/2-SUM(a) OVER(ORDER BY a)b /*2. Calculates the sum of values which are missing from the consecutive sequence
between 1 and a (both inclusive).
This utilises the default behaviour of the windowed function. This defaults to:
SUM(a) OVER(ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
Which is far more descriptive, but too verbose for golf :).*/
FROM @t
GROUP BY a --1. Removes duplicate occurences of a, as this is just noise anyway...
)X --necessary alias
GROUP BY b -- 3. Group by b as these are the groups we want to know the size of
)Y --necessary alias
This algorithm does the following:
- Remove duplicate elements (essentially turn a list into a set)
- Order the elements
- Find all the consecutive sequences
- Prescribe a unique identifier for each consecutive sequence
- Get the count of elements in each distinct sequence (length of each sequence)
- Get the maximum count.
In my opinion the interesting part of this is prescribing the unique identifier. Let's generalise the problem, starting with an arbitrary subset of the natural numbers.
$$X \subseteq \mathbb N_{\ne 0}$$
Consider the following binary relation
$$R = \{(a, b): |a - b| \leq 1, (a, b) \in X \times X\}$$
R is the set of all ordered pairs which are no more than 1 distance apart.
Here the distance is taxicab or L1 or whatever you want to call it.
It's fairly easy to see that R is symmetric, and reflexive, but NOT transitive.
Take the example $$X = \{1, 2, 3, 5, 6, 7, 8\}$$ $$(1, 2) \in R , (2, 3) \in R , (1, 3) \notin R$$
You can't have three distinct natural numbers all within a distance of 1 from eachother.
We need to extend the definition of R so that it is transitive. We need to find
the transitive closure of R.
$$R^{+} = R \cup \{(a, c): (a, b) \in R \wedge (b, c) \in R\}$$
Now we have an equivalence relation, and it properly describes the equivalence classes that underly this problem. However we still need to determine a label for each of these equivalence classes. Normally the smallest positive representitve of each equivalence class is chosen. This would be [1] and [5] in the example above. But these are just labels, as long as they are unique they could be anything. Let's make a function that sufficiently labels an element's equivalence class.
$$Label(i) = \sum_{k=1}^{i} k - \sum_{j\in X \wedge j\leq i}{j}$$
We can replace the first sum with the equivalent expression $$ \sum_{k=1}^{i} k = \frac{i(i + 1)}{2}$$
Thus, $$Label(i) = \frac{i(i + 1)}{2} - \sum_{j\in X \wedge j\leq i}{j}$$
Intuitively this is just the sum of everything 'missing' up to the current element.
$$Label(i) = \sum_{j \in \mathbb N_{\ne 0} \setminus X \wedge j \leq i} j$$
Now i am really interested, is there a generalisation of this? That is to say if we take the base relation R and redefined it:
$$R = \{(a, b): |a - b| \leq k, (a, b) \in X \times X\}, k \in \mathbb N_{\ne 0}$$
Would there be an equivalent intuitive 'label function' for any value of k?
Rust, 74 bytes
|a:&[u8]|(*a.iter().min().unwrap()..).take_while(|x|a.contains(x)).count()
Explanation:
let f = |a: &[u8]| {
// create an iterator starting from the least element
(*a.iter().min().unwrap()..)
// take item if in list, stopping at the first item not in list
.take_while(|x| a.contains(x))
// get total number of items taken
.count()
};
julia, 24 22 bytes
Edit: 2-byte improvement due to MarcMush
Same approach as most others here, counting how many times v can be intersected with v-1 before we get an empty list
!v=v>[]&&!(v∩.~-v)+1
How?
!v =
v > [] && # return 0 if v == [] and terminate, otherwise proceed
!(v ∩ .~ -v) + 1 # recurse on the intersection of v with v + 1 and add 1
to count recursion depth. Here, ~ is bitwise not, so
~(-k) == k-1 for signed integers.
Scala 3, 74 73 70 bytes
Port of @xnor's Python answer in Scala.
Thanks to @corvus_192's comment and @Kjetil S's comment.
def f(l:Seq[Int]):Int=if(l.size<1)0 else 1+f(l.filter(l contains _+1))
Perl 5, 32 bytes
sub f{@_?1+f(grep$_+1~~@_,@_):0}
Copied idea from others here. Return count of rounds of removal of ints n where n+1 dont exists before list is empty.
Charcoal, 23 19 13 bytes
IL⌈⪪⭆⊕⌈θ¬№θι1
Try it online! Link is to verbose version of code. Explanation: Port of @JonathanAllan's Jelly answer, with the "untruth" replaced by a map over range using 0 for present and 1 for absent as this lets me split on 1s to find the longest run of 0s. (I can't split on 0s as this will fail if there are 10 of the same value in the list.)
θ Input list
⌈ Maximum
⊕ Incremented
⭆ Map over implicit range and join
№ Count of
ι Current value
θ In input list
¬ Logical Not
⪪ 1 Split on `1`s
⌈ Take the longest run of `0`s
L Take the length
I Cast to string
Implicitly print
Previous 19-byte solution also works with negative integers:
I⌈Eθ⌈Eθ∧¬⁻…·ιλθ⊕⁻λι
Try it online! Link is to verbose version of code. Explanation: Creates inclusive ranges between all pairs of input elements and outputs the length of the longest range that is a subset of the input.
θ Input array
E Map over elements
θ Input array
E Map over elements
…· Inclusive range from
ι Outer value to
λ Inner value
⁻ Remove elements found in
θ Input array
¬ Is empty
∧ Logical And
λ Inner value
⁻ Minus
ι Outer value
⊕ Incremented
⌈ Take the maximum
⌈ Take the maximum
I Cast to string
Implicitly print
Google Sheets, 61 bytes
=sort(max(len(split(join(,frequency(A:A,sequence(9^4))),0))))
Put the input in cells A1:A and the formula in B1.
Uses frequency() like the Excel answer. Does not require CSE.
C (clang 16), 93 bytes
Using C23 _BitInt(65536) as a clang extension in C89 for implicit int. Works for input integers in the range [0,65535], e.g. 16-bit unsigned (but it takes them as an array of int, with an int n length). It returns the count.
typedef _BitInt(1<<16)T;f(*p,n){T b=0;for(;n--;)b|=(T)1<<p[n];for(n=1;b&=b<<1;n++);return n;}
First loop: Add each input to a set implemented as a bitmap (bitset |= 1<<n). Second loop: treating that as a wide integer, count the longest run of set bit. (Shift and AND to remove the low bit from each run of bits, count iterations until the whole thing becomes zero.)
Godbolt including a test caller with the test cases from the question.
See Add two really big numbers re: _BitInt. Clang 16 supports very large widths: the current max is 8388608 bits for signed or unsigned, _BitInt(1<<23). So not enough to handle values up to INT_MAX, but we don't handle negative int values either.
And yes, shifting and ANDing a 64KiB bitmap as a wide integer takes a ridiculous amount of asm instructions since clang fully unrolls it. I used _BitInt(1<<8) while developing this to keep edit/compile/run cycles short. With 1<<16 width, clang16 -std=gnu89 -Os -Wall -Wno-implicit-int makes 24249 lines of asm for x86-64 (only a couple of them labels instead of instructions.) It also uses about 30472 + 128 (red zone) = 30600 bytes of stack space. (64Kib = 8KiB)
Ungolfed:
typedef _BitInt(1<<8) T; // narrower for testing with small number inputs
/* int */ ungolfed(int *p, int n){
T bitset = 0;
for(;n--;){
bitset |= (T)1<<p[n]; // cast needed *before* the shift
}
int retval=1; // reuse n for this in the golfed version
// https://stackoverflow.com/questions/3304705/finding-consecutive-bit-string-of-1-or-0
while(bitset &= bitset<<1){ // doesn't count the iteration that clears the last bit, hence retval=1 to start
retval++;
}
return retval;
}
With clang -fsanitize=undefined, it will reject out-of-bounds inputs, e.g. runtime error: shift exponent 200000 is too large for 65536-bit type 'T' (aka '_BitInt(65536)')
Python, 122 113 101 52 51 bytes
Port of Arnaulds answer
lambda a:max(map(g:=lambda v:v in a and-~g(v+1),a))
Previous Answer
lambda a:max(len([*l])for b,l in groupby(r in a for r in range(max(a)+1))if b)
from itertools import*
JavaScript (ES6), 51 bytes
a=>Math.max(...a.map(g=v=>a.includes(v)&&1+g(v+1)))
Commented
a => // a[] = input array
Math.max(... // return the maximum value in this:
a.map(g = v => // for each value v in a[],
// using a recursive callback function g:
a.includes(v) && // if a[] includes v:
1 + // increment the final result
g(v + 1) // and do a recursive call with v + 1
) // end of map()
) // end of Math.max()
JavaScript (ES6), 56 bytes
This version was originally shorter (50 bytes), but invalid (as pointed out by Dominic van Essen). Now fixed at the cost of 6 bytes by adding p^v?...:0, which makes it uncompetitive.
Expects an Uint32Array.
a=>a.sort().map(p=v=>p^v?p+1^(p=v)?i=1:m+=++i>m:0,m=1)|m
Nibbles, 5.5 5 bytes (10 nibbles)
,|`.$`&+~$
Port of ovs' Jelly answer.
,|`.$`&+~$ # full program
,|`.$`&+~$$ # with implicit arg:
`. # iterate while unique
$ # starting with input:
+~$ # add 1 to every element
`& # intersection with original list
| # now, filter this list-of-lists
$ # to include only non-empty lists
, # and return its length
Excel, 65 bytes
=MAX(LEN(TEXTSPLIT(CONCAT(FREQUENCY(A1#,SEQUENCE(MAX(A1#)))),0)))
Input is spilled vertical array in A1#.
05AB1E, 6 bytes
ZLåγOà
Port of @JonathanAllan's Jelly answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
Z # Push the maximum of the (implicit) input-list (without popping)
L # Pop and push a list in the range [1,max]
å # Check for each value whether it's in the input-list (1 if truthy; 0 if falsey)
γ # Group adjacent equal bits together
O # Sum each inner list
à # Pop and push the maximum
# (which is output implicitly as result)
Since I was curious: a port of @ovs' Jelly answer is 8 bytes instead:
.ΓÐ<åÏ}g
Try it online or verify all test cases.
Explanation:
.Γ } # Continue until the list no longer changes, keeping all intermediate results
# (excluding the initial value), using the (implicit) input-list
Ð # Triplicate the current list
< # Pop the top copy, and decrease all its values by 1
å # Pop it and another list, and check for each value-1 whether it's in thelist
Ï # Pop both again, and keep all values at the truthy positions
g # After the changes-loop: pop and push its length
# (which is output implicitly as result)
Factor + math.extras, 51 bytes
[ sort members [ - -1 = ] max-monotonic-count 1 + ]
Sort, uniquify, then count the longest run of two adjacent numbers differing by one.
J, 22 bytes
[:#[:+/ .*~^:a:~1=-//~
We view this is a graph problem. Let's consider the example 10, 9, 8, 7, 6, 5 for the explanation:
1=-//~Create an adjacency matrix, where two numbers are connected if their difference is 1 (signs matter):0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0This says that 10 connects to 9, 9 connects to 8, etc.
[:+/ .*~^:a:Multiply this matrix by itself until we reach a fixed point. One self-multiplication, eg, tells us the connections after two steps:0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0After two steps, 10 connects to 8, 9 to 7, etc. Then two self multiplications show us the connections after 3 steps, and so on. Eventually, we'll reach a matrix of all zeroes and stop, and the length of this chain tells us the length of the longest consecutive run of numbers.
[:#So we return that length.
J, port of ovs's answer, 21 20 bytes
1#@}.((e.>:)#[)^:a:~
Nekomata, 7 bytes
ˡ{N→ᵈ:∩
A port of @ovs's Jelly answer.
ˡ{N→ᵈ:∩
ˡ{ Loop until failure, and return the number of iterations
N Check if the list is nonempty
→ Increment
ᵈ:∩ Intersect with the input
Vyxal, 35 bitsv2, 4.375 bytes
ÞǔĠṠG
How?
Port of my Jelly answer.
ÞǔĠṠG - list of positive integers e.g. [4, 8, 3, 4, 2, 7, 4]
Þǔ - unthruth [0, 1, 1, 1, 0, 0, 1, 1]
( i.e.: 2 3 4 7 8 )
Ġ - group runs of equal elements [[0], [1, 1, 1], [0, 0], [1, 1]]
Ṡ - sums [0, 3, 0, 2]
G - maximum 3
Jelly, 5 bytes
ṬŒg§Ṁ
A monadic Link that accepts a list of positive integers and yields the length of the longest consecutive sequence.
How?
ṬŒg§Ṁ - Link: list of positive integers, A e.g. [4, 8, 3, 4, 2, 7, 4]
Ṭ - untruth {A} [0, 1, 1, 1, 0, 0, 1, 1]
( i.e.: 2 3 4 7 8 )
Œg - group runs of equal elements [[0], [1, 1, 1], [0, 0], [1, 1]]
§ - sums [0, 3, 0, 2]
Ṁ - maximum 3
Python 3, 45 bytes
f=lambda l:len(l)and-~f({*l}&{x-1for x in l})
Without using sets:
Python, 48 bytes
f=lambda l:l>[]and-~f([x for x in l if-~x in l])
Repeatedly filters l by only keeping elements x where x+1 is also in l, and counts how many repetitions until the list is empty.
Haskell, 32 bytes
f[]=0
f l=1+f[n|n<-l,elem(n+1)l]
Repeatedly filters l by only keeping elements n where n+1 is also in l, and counts how many repetitions until the list is empty.
Jelly, 6 bytes
f‘$ƬL’
Ƭ -- iterate until the result doesn't change, collecting intermediate value
f‘ -- keep the values that are still in the list when each value is incremented
L -- take the length (number of intermediate values, including the initial list and the empty list at the end)
‘ -- decrement
Retina 0.8.2, 51 bytes
$
,
\d+
$*
D`1+,
O`1+
(\G1+, |1\1)+
$#1¶
O#^`
1G`
Try it online! Link includes test cases. Explanation:
$
,
Suffix a comma to the last entry too.
\d+
$*
Convert to unary.
D`1+,
Deduplicate.
O`1+
Sort ascending.
(\G1+, |1\1)+
$#1¶
Count consecutive sequences. The first alternation is only possible at the start of the match, while the second alternation is only possible on subsequent repetitions of the capture group.
O#^`
Sort descending numerically.
1G`
Take the first (i.e. largest).
Vyxal G, 56 bitsv2, 7 bytes
ṗ's¯1=A;@
Explanation
ṗ's¯1=A;@ '# Implicit input
ṗ' ; '# Filter the powerset by:
s¯ # Sort and get deltas
1=A # All equal to 1?
@ # Length of each inner list
# Take the maximum
# Implicit output