An array contains N elements, each element within the range 1 <= K <= X where X <= N. The elements are in no particular order, but there are many duplicate elements. I am wondering if there is a way to iterate over the array, stopping as soon as one of every integer 1-X has been seen, and doing so without allocating an additional array and without sorting the original array.
Edit: The array is guaranteed to contain all values 1-X. X is a parameter, not a constant.
Edit:
More generally, I am wondering if there is some known way of combining unique consecutive integers into a sum or a product, such that the value of that sum or product can tell whether or not one of those integers is a part of it. Something like multiplying unique primes together.
Thanks