Consecutive Numbers LeetCode Solution

Problem – Consecutive Numbers LeetCode Solution

SQL Schema

Table: Logs

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| num         | varchar |
+-------------+---------+
id is the primary key for this table.
id is an autoincrement column.

Write an SQL query to find all numbers that appear at least three times consecutively.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: 
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1  | 1   |
| 2  | 1   |
| 3  | 1   |
| 4  | 2   |
| 5  | 1   |
| 6  | 2   |
| 7  | 2   |
+----+-----+
Output: 
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
Explanation: 1 is the only number that appears consecutively for at least three times.

Consecutive Numbers LeetCode Solution in Oracle

SELECT DISTINCT num AS ConsecutiveNums FROM logs l WHERE 
l.num = (SELECT num FROM logs WHERE id = l.id+1) AND l.num = (SELECT num FROM logs WHERE id = l.id+2)

Consecutive Numbers LeetCode Solution in MS SQL Server

SELECT DISTINCT Num as ConsecutiveNums
FROM (SELECT Num, 
             LEAD(num) OVER(ORDER BY Id) as 'Lead',
             LAG(num) OVER (ORDER BY Id) as 'Lag'
      FROM Logs) temp
WHERE Num = Lead and Num = Lag 
;

Consecutive Numbers LeetCode Solution in MySQL

SELECT DISTINCT l1.Num as ConsecutiveNums
FROM
    Logs l1,
    Logs l2,
    Logs l3
WHERE
    l1.Id = l2.Id - 1
    AND l2.Id = l3.Id - 1
    AND l1.Num = l2.Num
    AND l2.Num = l3.Num
;
Consecutive Numbers LeetCode Solution Review:

In our experience, we suggest you solve this Consecutive Numbers LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Consecutive Numbers LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Consecutive Numbers LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.