#17MediumSQL· Window Functions
Consecutive Numbers
Input format
Logs rows as [id, num]
Output format
Result rows as [ConsecutiveNums], ordered ascending; an empty array means no rows
Examples
Input: Logs = [[1, 4], [2, 4], [3, 4], [4, 2], [5, 4], [6, 2], [7, 2]]
Output: [[4]]
4 appears in rows 1, 2 and 3. The value 2 appears three times but never three rows in a row.
Input: Logs = [[1, 5], [2, 5], [3, 5], [4, 1], [5, 5], [6, 5], [7, 5]]
Output: [[5]]
5 has two separate runs of three, but is reported once.
Constraints
- 0 <= rows in Logs <= 10^5
- id values are unique; they are consecutive integers in the tests, but a robust query should only rely on their order.
- -10^6 <= num <= 10^6
Study first
- #sql
- #database
- #window-functions
