Created
April 22, 2017 06:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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