Python Program to Find Longest Consecutive Subsequence
Longest Consecutive Subsequence
Here on this page, we will learn to create Python Program to find Longest Consecutive Subsequence.
Example :
- Input : [7, 8, 1, 5, 4, 3]
- Output : 3
Algorithm
- Initialize Empty array val and a variable c with value zero
- Iterate using a for loop between range zero to l with variable i
- Initialize a variable n with value 1
- Use a nested while loop until arr[i]+n in arr
- for each iteration increment the value of c & n by 1
- Append c+1 in val
- set value of c to 0
- print maximum of val
Python Code
Run
def LongestConseqSubseq(arr, l):
val = []
c = 0
for i in range(l):
n = 1
while arr[i] + n in arr:
c += 1
n += 1
val.append(c + 1)
c = 0
return max(val)
array = [7, 8, 1, 5, 4, 3]
print("Longest Consecutive Subsequence :", LongestConseqSubseq(array, len(array)))
Output
Longest Consecutive Subsequence : 3
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
For similar questions click on the given button.

Login/Signup to comment