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
dbfiddle
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
?
[1,2,3,4,2,2,2]
(prevents simply sorting, taking differences, and counting runs of 1s). \$\endgroup\$[3, 5, 7, 9, 11]
(all step sizes equal) and[7, 7, 6, 6, 5, 5]
(multiple repeats in the longest consecutive sequence). \$\endgroup\$