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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
//
// Challenge
//
// You are given an unsorted array of integers @N.
//
// Write a script to find the longest consecutive sequence. Print 0 if
// none sequence found.
//
//
// For notes, see the perl solution: ../perl/ch-1.pl
//
//
// After reading the input, we put the numbers both in an array
// (called "array") and an object (called "set").
// We iterate over the array. If the element has been deleted
// from the set, we continue with the next element.
// Else, we construct the longest sequence this element is part
// off, by starting with the range containing just this element,
// and extending it on both sides as long as the extension is
// in the set. We then check whether this improves the best range
// seen so far (and update the best range), and delete the sequence
// from the set.
//
// When we have processed all numbers, we print the best sequence.
//
//
// Read STDIN. Split on newlines, then on whitespace, and turn the results
// into numbers.
//
let lines = require ("fs")
. readFileSync (0) // Read all.
. toString () // Turn it into a string.
. split ("\n") // Split on newlines.
;
//
// Iterate over the lines
//
for (let i = 0; i < lines . length - 1; i ++) {
//
// The line is a set of integers, split on
// white space, and store the numbers in a hash.
//
let array = lines [i] . split (" ") . map (_ => +_);
let set = array . reduce ((acc, val) => {
acc [val] = 1;
return acc;
}, {});
let best = [0, 0];
for (let i = 0; i < array . length; i ++) {
let low = array [i];
if (!set [low]) {
continue; // Skip if it's no longer in the set.
}
let high = low;
while (set [low - 1]) {
low --;
}
while (set [high + 1]) {
high ++;
}
if (low < high && // Exclude sequences of length 1
high - low > best [1] - best [0]) {
best = [low, high];
}
for (let i = low; i <= high; i ++) {
set [i] = 0;
}
}
//
// Output
//
let str = "";
for (let i = best [0]; i <= best [1]; i ++) {
str += i;
if (i < best [1]) {
str += ", ";
}
}
console . log (str);
}
|