Skip to content

ORIGINAL CODING QUESTION

Longest echo in a skill log

A skill log is a sequence of integer codes. Return the length of the longest subsequence that reads the same from both ends. A subsequence keeps the original order but may skip codes.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n codes.

Constraints

1 ≤ n ≤ 30; 0 ≤ code ≤ 9.

Example 1

Input

7 1 4 2 3 2 5 1

Output

5

The subsequence 1,2,3,2,1 has length five.

Example 2

Input

3 1 2 3

Output

1

Any single code is a length-one echo.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Think about the best answer inside each range [i, j].

Hint 2

If the end codes match they can wrap the best answer of [i+1, j-1].

Hint 3

Otherwise drop one end: best(i, j) = max(best(i+1, j), best(i, j-1)).

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →