Possible Duplicate:
Check if array B is a permutation of A
Is there a way to tell if two arrays of numbers (which can contain positives, negatives or repeats) are permutations of each other in O(n) time complexity and O(1) space complexity? I could not solve it because of tight space constraints.
If the numbers are integers – in-place radix sort can give you
O(nlogk)time, wherekis the range of the numbers, andnis the number of elements.Note that the algorithm requires
O(logk)space, for the stack trace of recursive calls.If you can bound
kto a constant (2^64 for example) – you getO(n)time withO(1)space.After sorting – you can simply iterate on both arrays and check if they are identical.