I found this interview question, and I couldn’t come up with an algorithm better than O(N^2 * P):
Given a vector of P natural numbers (1,2,3,…,P) and another vector of length N whose elements are from the first vector, find the longest subsequence in the second vector, such that all elements are uniformly distributed (have the same frequency).
Example : (1,2,3) and (1,2,1,3,2,1,3,1,2,3,1). The longest subsequence is in the interval [2,10], because it contains all the elements from the first sequence with the same frequency (1 appears three times, 2 three times, and 3 three times).
The time complexity should be O(N * P).
“Subsequence” usually means noncontiguous. I’m going to assume that you meant “sublist”.
Here’s an O(N P) algorithm
assuming we can hash(assumption not needed; we can radix sort instead). Scan the array keeping a running total for each number. For your example,Now, normalize each row by subtracting the minimum element. The result is
Prepare two hashes, mapping each row to the first index at which it appears and the last index at which it appears. Iterate through the keys and take the one with maximum last – first.
The best key is 100, since its sublist has length 9. The sublist is the (1+1)th element to the 10th.
This works because a sublist is balanced if and only if its first and last unnormalized histograms are the same up to adding a constant, which occurs if and only if the first and last normalized histograms are identical.