#K11926. Longest Contiguous Subsequence with Two Distinct Integers

    ID: 23577 Type: Default 1000ms 256MiB

Longest Contiguous Subsequence with Two Distinct Integers

Longest Contiguous Subsequence with Two Distinct Integers

Given a list of integers, your task is to find the length of the longest contiguous subsequence that contains at most two distinct integers. More formally, for an array of integers \(\textbf{nums}\), you need to determine the maximum length \(L\) such that there exists a subarray of length \(L\) where the number of distinct integers is no more than two.

The problem can be approached using the sliding window technique. If the array is empty, the answer is 0.

Constraints:

  • \(0 \leq n \leq 10^5\), where \(n\) is the number of elements in the array.
  • The integers can range from \(-10^9\) to \(10^9\).

inputFormat

The input consists of two lines. The first line contains an integer \(n\), representing 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 representing the length of the longest contiguous subsequence that contains at most two distinct integers.

## sample
7
1 2 1 2 1 3 4
5