Javatpoint Logo
Javatpoint Logo

Longest Consecutive Subsequence

Introduction

Within the field of algorithmic problem solving, it is common practice to look for patterns and sequences in datasets. Finding the Longest Consecutive Subsequence in an array of integers is one interesting problem. A sequence of integers with consecutive numbers as elements-albeit not always in sorted order is called a consecutive subsequence. Finding the length of the longest such subsequence in the provided array is the aim.

This problem captures a larger theme in mathematics and computer science, which is the identification and comprehension of patterns in data. The longest consecutive sequence problem needs to be solved strategically, striking a balance between clarity and efficiency. We'll explore a variety of approaches to solving this issue, from the basic brute force method to more efficient ones like sorting the array or making use of data structures like HashSet. Numerous approaches offer distinct benefits and factors to take into account, creating an abundant terrain for algorithmic problem-solving.

Method 1: Brute Force Approach

One of the simplest approaches to problem solving is the brute force method, which entails methodically examining every potential solution in order to identify the right one. The brute force method looks at every possible subsequence to find the one with the longest length when attempting to find the longest consecutive subsequence in an array of integers.

Code

Output:

Longest Consecutive Subsequence

Code Explanation

Maximum Function

  • A useful function that yields the highest value between two integers.

Longest Consecutive Subsequence Function

  • Accepts as input an array arr with length n.
  • Sets maxLength's initial value to 1, which is the smallest length that a subsequent subsequence can have.

Outer Loop (for loop)

  • Uses the index i to iterate through each element in the array.
  • An inner loop is started for each element at index i.

Inner Loop (for loop within the outer loop)

  • To find consecutive elements, begin with the following element (j = i + 1).
  • CurrentLength is increased if the current element arr[j] is one more than the previous element arr[j - 1].
  • The inner loop breaks if the preceding sequence is broken (the current element is not one greater than the previous).

Revising maxLength

  • Compares the length of the current iteration to its maximum length.
  • Update maxLength to reflect the maximum of currentLength and its current value.

Main Function

  • Determines the length n and initializes an array arr with values {100, 4, 200, 1, 3, 2}.
  • Uses the array's length and the longestConsecutiveSubsequence function to call it.
  • Prints the outcome, which is the longest consecutive subsequence's length.

Output

  • Prints to the console the length of the longest consecutive subsequence.

Time Complexity

  • Because of the nested loops, the code has an O(n^2) temporal complexity, where 'n' is the number of elements in the array.

Method 2: Sorting

In computer science, sorting is a common technique that entails putting elements in a particular order. Sorting the array provides an efficient and elegant way to find the longest consecutive subsequence in the array. This approach makes use of the fact that, following sorting, consecutive elements in the subsequence will be next to one another.

Code

Output:

Longest Consecutive Subsequence

Code Explanation

Functional Prototypes

  • At the outset, the function prototypes are compared, and the max is declared.
  • Max is a useful function for determining the maximum of two numbers, while compare is a comparator function used for sorting.

Function of Comparator (compare)

  • The qsort function sorts the array in ascending order using the compare function.
  • It accepts two pointers to different elements (a and b) and outputs the value difference.

Utility Function Maximum

  • The utility function max returns the maximum of two integers, a and b when given two of them.

Main Function (longestConsecutiveSubsequence)

  • The main function determines the length of the longest consecutive subsequence in an array of values.
  • It returns 0 if the array is empty (n == 0).

Sorting the Array

  • Using the qsort function, the array (arr) is sorted in ascending order.
  • The comparator for sorting is the compare function.

Tracking Consecutive Subsequences

  • The code searches for consecutive elements as iteratively goes through the sorted array.
  • An element is a part of the current consecutive subsequence if it is the same as or greater than the previous element.
  • In accordance, the length of the succeeding subsequence (currentLength) is increased.

Handling Duplicates

  • The code makes sure that unique elements add to the length of successive subsequences and checks for duplicates.

Updating Maximum Length

  • Every time a longer consecutive subsequence is discovered, the maximum length (maxLength) is updated.

Returning Result

  • The function gives back the longest consecutive subsequence that could be found in the sorted array.

Main Function(main)

  • An example array {100, 4, 200, 1, 3, 2} is used in the main function.
  • The length of the longest consecutive subsequence is displayed when the result is printed using printf.

Method 3: HashSet

The HashSet method provides an effective way to find the longest consecutive subsequence in an integer array. A data structure called a hash set enables fundamental operations like insertions, deletions, and lookups to be performed with constant-time average complexity. An array that prevents duplicate elements is called a hash set. It is created by mapping values to array indices using a hash function. Whenever an element is added to the hash set, its hash code is calculated, and it is inserted into the corresponding index. This makes quick lookups possible because it can be quickly ascertained whether an element is present by comparing its corresponding index.

Code

Output:

Longest Consecutive Subsequence

Code Explanation

Header Inclusions

  • The standard input-output library (#include) is included in the program.

Macros

  • Using macros, the boolean values bool, true, and false are defined.

Maximum Function

  • The max Function returns the maximum of two integers.

Function with the longest consecutive subsequence

  • The parameters of the function longestConsecutiveSubsequence are an array arr and its size n.
  • A HashSet (hashSet) array is initialized in order to track unique elements.
  • While traversing the array, the function sets each corresponding indices in hashSet to 1.
  • The array is then iterated through once more to determine the beginning of each subsequent subsequence where the preceding element is absent from the HashSet.
  • It counts the subsequent elements for each of these starting elements in ascending order, updating the maximum length in the event that a longer subsequence is discovered.
  • The length of the longest consecutive subsequence is the result.

Main Function

  • The following defines an example array arr: {100, 4, 200, 1, 3, 2}.
  • The array n's size is computed.
  • The array and its size are passed to the longestConsecutiveSubsequence function, and the outcome is kept in the result variable.
  • After that, the console prints the outcome.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA