Longest Distinct Stretch

easy arrays hashing sliding-window two-pointers 1000 ms · 256 MB

Longest Distinct Stretch

You are given an array of n integers a_1, a_2, ..., a_n.

A stretch is a contiguous segment a_l, a_{l+1}, ..., a_r with 1 ≤ l ≤ r ≤ n. A stretch is distinct if no value appears in it more than once.

Find the length of the longest distinct stretch.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·10^5).

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).

Output

Print one integer: the largest value of r − l + 1 over all distinct stretches.

Notes

In the first example, the longest distinct stretch is 4 1 5 9 2 6 (positions 3 to 8), of length 6. Extending it to the left would add a second 1, and extending it to the right would add a second 5.

In the second example, all values are equal, so every distinct stretch has length 1.

Example 1

Input

10
3 1 4 1 5 9 2 6 5 3

Output

6

Example 2

Input

5
7 7 7 7 7

Output

1

Hints · reveal one at a time if you're stuck

Hint 1

Checking every stretch is O(n²), which is too slow for n = 2·10^5. If a stretch is distinct, what can you say about every stretch inside it? As the right end moves, does the best left end ever need to move back?

Hint 2

Keep a window whose left end only moves right, and remember the last position of each value. When a value repeats inside the window, move the left end just past its previous occurrence, but never move it backwards. Record the window length at each step.

report an issue Prefer your own editor? sooks prep 7 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.