Longest Neat Subsequence (Concatenation of Blocks)

viaGlassdoor

Problem: An array is called 'neat' if it can be formed by concatenating an arbitrary number of identical 'blocks' (an empty array counts as neat). Given an array a of n integers, find the length of its longest subsequence that is neat.

Constraints: n can be large enough that brute-force enumeration of subsequences is infeasible; requires reasoning about block length and repetition.

Example: For an array with a repeating pattern subsequence, the longest neat subsequence is the longest subsequence expressible as some block repeated k>=1 times (or the empty subsequence, trivially neat).

Approach: For each candidate block length L, try to greedily extract the longest subsequence matching some fixed block of length L repeated as many times as possible, tracking positions consumed so the same index isn't reused; take the maximum over all candidate block lengths and block contents. Because L can range broadly, an efficient solution typically bounds the search (e.g., only small block lengths matter for the optimal answer in many test regimes) or uses DP over (position, phase-within-block) to find the best matching repetition efficiently.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account