You can see that each element is searched at most two times. Since the time complexity of the search operation is now O(1), we will just need to check how many times that operation will be performed. This checks if the current element is the first Pseudo-Code int longestConsecutiveSequence(int A, int n)
Consecutive numbers help update#
Update longest_streak if curr_streak is bigger than it.ĥ. If it does, then it is not the first element of its corresponding sequence.Check for consecutive numbers greater than A in the hash table and keep on increasing curr_streak. If it does not exist, then it is the first element of its corresponding sequence.Linearly iterate over the array and check if A -1 exists.Create a Hash Table of size n and add elements of the array in it.Declare longest_streak and initialize to 0.We can improve the time complexity of searching consecutive elements by using a hash table which can check the presence of consecutive elements in O(1) average. is this algorithm works fine if elements are repeated?.Why is curr_streak initialized to 1 and not 0?.Why is the condition iIf a number is traversed while searching for streak for some previous smaller number, is it revisited ever again in this algorithm?.Why is the loop variable ‘i’ incremented when curr_streak is equal to 1 outside the while loop?.Space Complexity: If we use Merge Sort, O(n), else if we use Heap Sort, O(1) Critical ideas to think! Time Complexity: Sorting an array + Linear Traversal of the array (Even though nested loops exist, both loops use same loop variable which is increased linearly) = O(nlogn) + O(n) = O(nlogn) Linearly check for the longest consecutive sequence now. Now, the consecutive elements will be linearly lined-up next to each other. Can we improve the time complexity of searching for nearby elements?.What if we use a visited array to track elements included in any streak so that they are not computed again? How will this affect the complexity?.Space Complexity: O(1) Critical ideas to think! Therefore, inside the outer loop, the worst-case time complexity is O(n²).Īlso, this process is repeated for each element of the array. This search will be done as long as the streak will be and the longest a streak can be is the size of the array, i.e., n. The inner loops linearly search for consecutive elements and the worst-case time complexity for linear search is O(n). Longest_streak = max(longest_streak, curr_streak) While( linear_search(A,n,curr_num) = True ) Update longest_streak if curr_streak is greater.Do the same for consecutive elements greater than the current element.Search for consecutive elements smaller than the current element and increase current streak accordingly.Declare a curr_streak variable that stores the longest streak that can be made with the current element as a part of it.Declare and initialize longest_streak variable to 0.
![consecutive numbers help consecutive numbers help](https://instasolv1.s3.ap-south-1.amazonaws.com/QuestionBank/5f34f4981cd8ac416a4e9238/solution_5f34f4981cd8ac416a4e9239.png)
Keep a track of current streak and the longest streak of consecutive elements in the array. Sorting and checking the longest streak of consecutive elementsįor each element in A, linearly search for consecutive elements greater and lesser than the current element.Brute Force: Using nested loops to search the next element.We will be discussing three ways to solve the problem:. Can the array contain duplicates? Ans: Sure, that's a possibility.Are the array elements necessarily positive? ( Ans: No, they can be positive, negative, or zero).Does the sequence found necessarily be increasing or decreasing in nature? ( Ans: No, the sequence can be in any order but the elements must be consecutive integers when sorted).Can the array be modified in any way? ( Ans: Yes).Possible follow-up questions to ask the interviewer :
![consecutive numbers help consecutive numbers help](https://my-live-05.slatic.net/p/5db4241298d98272fd515029ba821dc0.jpg)
Problem Description: Given an unsorted array A consisting of n integers, you need to find the length of the longest consecutive sequence of integers in the array.Įxample 1 Input: A = Įxplanation: The longest consecutive sequence of integers in the array is 1,2,3 and 4Įxample 2 Input: A = Įxplanation: The longest consecutive sequence of integers in the array is -2,-1,0,1,2, and 3.
![consecutive numbers help consecutive numbers help](https://aws1.discourse-cdn.com/business5/uploads/percona1/original/2X/d/d739dcd714a21647a91d9c7d10a113321ceb1d5a.jpeg)
![consecutive numbers help consecutive numbers help](https://miro.medium.com/max/890/1*mS7_Mp7fmIVTC_9AYSOwkw.png)
Difficulty: Hard Asked in: Amazon, Google Understanding the problem