From 8fc250fa1105084b85ed1e2267fd61dbb884023c Mon Sep 17 00:00:00 2001 From: Shobhit Saini <108419442+Shobhit0603@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:33:03 +0530 Subject: [PATCH] pull request for creating python code for longest consecutive sequence by issue number #9436 --- Longestconsecutivesequence.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Longestconsecutivesequence.py diff --git a/Longestconsecutivesequence.py b/Longestconsecutivesequence.py new file mode 100644 index 000000000000..0620c5ff339e --- /dev/null +++ b/Longestconsecutivesequence.py @@ -0,0 +1,16 @@ +def longest_consecutive_sequence(arr): + heap = [] + for num in arr: + heapq.heappush(heap, num) + max_len = 0 + curr_len = 1 + while heap: + num = heapq.heappop(heap) + if heap and num == heap[0]-1: + curr_len += 1 + elif curr_len > 1: + max_len = max(max_len, curr_len) + curr_len = 1 + if curr_len > 1: + max_len = max(max_len, curr_len) + return max_len