#K62482. Longest Unique Subarray

    ID: 31541 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. In other words, find a subarray where no element appears more than once and it is as long as possible.

You can use a sliding window approach to solve the problem efficiently. If you let start be the index marking the beginning of the current window and end the current index, then the current window length is given by \(\text{end} - \text{start} + 1\). When a duplicate is encountered at index \(\text{end}\), update \(\text{start}\) as necessary. The answer is the maximum value of \(\text{end} - \text{start} + 1\) encountered during the iteration.

inputFormat

The input consists of two lines:

  • The first line contains a single integer \(n\) which represents the number of elements in the array.
  • The second line contains \(n\) space-separated integers representing the elements of the array. If \(n = 0\), then the second line may be omitted.

outputFormat

Output a single integer which is the length of the longest contiguous subarray that contains all unique elements.

## sample
5
1 2 3 2 1
3