Suppose you are given an array of length P that contains all natural numbers upto P and another array of length N that contains elements from the first array. Write an algorithm to find the longest subsequence in the second array so that the elements are uniformly distributed frequencywise.