Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 22, 2017 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/73056667cb2605696706dd93a7beee8e to your computer and use it in GitHub Desktop.
Save thinkphp/73056667cb2605696706dd93a7beee8e to your computer and use it in GitHub Desktop.
Given an array of integers A[], the task is to complete the function which returns an integer denoting the length of the longest sub-sequence such that elements in the sub-sequence are consecutive integers, the consecutive numbers can be in any order.
int findLongestConseqSubseq(int arr[], int n)
{
int vec[100000]={0},
max = 0;
for(int i = 0; i < n; i++){
if(arr[i] > max) max = arr[i];
vec[arr[i]] = 1;
}
int len = 0;
int sublen = 0;
for(int j = 0; j <= max; j++) {
if(vec[j] == 1) {
sublen++;
} else {
if(sublen > len) len = sublen;
sublen = 0;
}
}
if(sublen > len) len = sublen;
return len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment