FastPrepFastPrep
Problem Brief

Maximum Candies with At Most Two Types in a Line

FULLTIMEONSITE INTERVIEW
See Tiktok online assessment and hiring insights

Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem.

Problem

Given a line of candies represented by a 1D sequence candies, where each position has a candy type.

You may pick at most two distinct candy types, and the candies you pick must come from a contiguous segment: starting at some index and moving right, picking adjacent candies one by one.

Rule: once you are about to encounter a third distinct candy type, you must stop immediately (you cannot pick that candy or anything to its right).

Return the maximum number of candies you can pick under these rules (i.e., the length of the longest contiguous segment containing at most two distinct types).

Examples

Input: [1,2,1] Output: 3

Input: [1,2,3,2,2] Output: 4

Constraints (typical)

1 <= n <= 2*10^5

Candy types are integers

Expected: O(n) time

Example

Input

3

1 2 1

Output

3

Function Description

Complete solveMaximumCandiesTwoTypesLine. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters.

1Example 1

Input
input = "3\n1 2 1"
Output
["3"]
Explanation

The returned string array must match the expected standard output lines for the sample input.

Constraints

Limits and guarantees your solution can rely on.

Use the limits and requirements stated in the prompt.

public String[] solveMaximumCandiesTwoTypesLine(String input) {
    // write your code here
}
Input

input

"3\n1 2 1"

Output

["3"]

Sign in to submit your solution.