-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_consecutive_subsequence.cpp
More file actions
46 lines (37 loc) · 1.1 KB
/
longest_consecutive_subsequence.cpp
File metadata and controls
46 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#define MAX 100
int main() {
printf("Start\n");
int list[] = {10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44};
int size = sizeof(list) / sizeof(list[0]);
printf("Your list is: [");
for (int i = 0; i < size; i++) {
printf(" %d ", list[i]);
}
printf("]\n");
int longest[MAX];
int longestSize = 0;
int current[MAX];
int currentSize = 0;
for (int i = 0; i < size; i++) {
if (i > 0 && list[i] == list[i - 1] + 1) {
current[currentSize] = list[i];
currentSize++;
} else {
if (currentSize > longestSize) {
for (int j = 0; j < currentSize; j++) {
longest[j] = current[j];
}
longestSize = currentSize;
}
current[0] = list[i];
currentSize = 1;
}
}
printf("Longest consecutive subsequence of big numbers is: [");
for (int i = 0; i < longestSize; i++) {
printf(" %d ", longest[i]);
}
printf("]\n");
return 0;
}