Official Editorial (C++)

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N\log N)

Java

import java.io.*;
import java.util.*;
public class ConsecutiveSubsequence {
public static void main(String[] args) {
Kattio io = new Kattio();
int N = io.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++) { arr[i] = io.nextInt(); }

C++

#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> v(n);
for (auto &i : v) cin >> i;

Python

size = int(input())
arr = list(map(int, input().split()))
best_len = 1
# given the ending limit, stores the longest consecutive sequence
ending_max_len = {} # {element: (length, index)}
come_from = {} # {index: previous_index}
for i in range(size):
n = arr[i]
if (n - 1) not in ending_max_len:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!