Possible duplicate:Check if array B is a permutation A
Provided 2 unsorted array of integer aand bequal size. Determine if the bpermutation is a. Can this be done in O(n) timeand O(1) space?
a
b
O(n) time
O(1) space
The first thing that occurred to me was the use XOR, i.e. XOR all the elements of a and b and if the resultant is 0 which means that b is a permutation of a. But he gives examples where this approach fails. For example, for example,
XOR
XOR all the elements of a and b and if the resultant is 0 which means that b is a permutation of a
a: [1 6 0 0 4] -- b: [1 0 6 1 5] a: [1 6 0 0 5] -- b: [1 0 6 1 4]
Anyone who has any idea how to do this in O(n) timeand out O(1) space?
- [n,m] , m-n = U radix sort, .
[n,m]
m-n = U
, - - , .
:"" [ , , OP ..], O(nlogU), - O(logU). O(logU) = O(1), O(n) O(1) .
O(nlogU)
O(logU)
O(logU) = O(1)
O(n)
O(1)
(a BigInteger ), A:
BigInteger
A
C(A) = product(p_(a+1))) A A
C(A) = product(p_(a+1)))
p_n - n th . C A, ; C.
p_n
n
C
,
C([1 6 0 0 4]) = p_2.p_7.p_1.p_1.p_5 = 3.17.2.2.11 = 2244
(, , C, ),
C([1 6 0 1 4]) = p_2.p_7.p_1.p_2.p_5 = 3.17.2.3.11 = 3366
, . , , ( ) . , , . , . ...
OR -, - .
, -, ...
Gives hashes that are extremely unlikely to collide, so they can be considered as unique identifiers for integers. Git uses SHA-1 hashes to identify versions of source code, the likelihood that the chance of a collision is so low can be ignored.
Commutative (e.g. xor and plus) and probably associative, so the order of the elements does not change the resulting hash.
This second requirement is probably awkward. I spent a bit of time on Google, but just got scared of words like Quasigroup.