#K93492. Longest Unique Subarray

    ID: 38431 Type: Default 1000ms 256MiB

Longest Unique Subarray

Longest Unique Subarray

Given an array of integers, your task is to determine the length of the longest contiguous subarray in which all elements are unique.

This problem challenges you to optimize your solution, typically using a sliding window approach. Formally, given an array A of length n, find the maximum length L for any subarray A[i...j] such that every element in that subarray is distinct. The problem can be mathematically expressed as finding the maximum L satisfying:

\(L = \max_{0 \leq i \leq j < n} \{ j - i + 1 \; : \; A[i...j] \text{ contains no duplicates} \}\)

inputFormat

The input is provided via stdin and is formatted as follows:

  • The first line contains an integer n (0 ≤ n ≤ 105) indicating the number of elements in the array.
  • The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer to stdout which is the length of the longest contiguous subarray with all unique elements.

## sample
8
5 1 3 5 2 3 4 1
5