Skip to content

Longest consecutive sequence java implemenation #477

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danieldotwav
Copy link

@danieldotwav danieldotwav commented Jan 17, 2024

Pull Request Template

Description

The algorithm is designed to find the longest consecutive sequence in an array of integers. It uses a HashSet to efficiently track the elements of the array and determine the length of the longest consecutive sequence. The key steps are:

  1. Create a HashSet: Add all elements from the input array to a HashSet. This allows for O(1) time complexity for checking the existence of elements.

  2. Iterate through the HashSet: For each element in the HashSet, check if it's the start of a new sequence. This is done by checking if (element - 1) is not in the HashSet. If it's the start of a new sequence, proceed to the next step.

  3. Count Consecutive Elements: Starting from the current element, count the number of consecutive elements. This is done by incrementally checking for (currentElement + 1) in the HashSet, and incrementing the count each time a consecutive element is found.

  4. Update the Longest Streak: If the count of consecutive elements is greater than the current longest streak, update the longest streak.

  5. Return the Result: After iterating through all elements, return the longest streak found.

Put check marks:

Have you made changes in README file ?

  • Added problem & solution under correct topic.
  • Specified Space & Time complexity.
  • Specified difficulty level, tag & Note(if any).

How Has This Been Tested?

###Test Cases
To verify the efficacy of the code, the following test cases can be considered:

Normal Case:
Input: [100, 4, 200, 1, 3, 2]
Expected Output: 4 (since the longest consecutive sequence is [1, 2, 3, 4]).

Empty Array:
Input: []
Expected Output: 0 (as there are no elements).

Array with No Consecutive Numbers:
Input: [10, 5, 6]
Expected Output: 1 (since the longest consecutive sequence would be any of the single elements).

Array with Duplicates:
Input: [1, 2, 2, 3, 4]
Expected Output: 4 (ignoring the duplicate 2, the longest sequence is [1, 2, 3, 4]).

Large Range of Numbers:
Input: A large range of numbers with some gaps.
Expected Output: Length of the longest consecutive sequence in the given range.

Non-Sequential Array:
Input: [9, 1, 4, 7, 3]
Expected Output: 1 (as there are no consecutive sequences longer than a single number).

Array with Negative Numbers:
Input: [-1, -2, 0, 1, 2]
Expected Output: 4 (sequence [-2, -1, 0, 1]).

Make sure all below guidelines are followed else PR will get Reject:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code so that it is easy to understand
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants