Comments on: Check if two arrays are permutation of each other https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/ Coding / System Design Interviews Sat, 19 Jan 2019 07:29:04 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Leia https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1975 Sat, 19 Jan 2019 07:29:04 +0000 http://www.ritambhara.in/?p=2852#comment-1975 As a variation of solution-1:
Can sum of values and sum of squares be a good way of comparing instead of comparing the products and sums?
sum_of_squares1 = sum_of_squares2
AND
sum1 = sum2

]]>
By: Sam https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1974 Wed, 18 Oct 2017 18:01:40 +0000 http://www.ritambhara.in/?p=2852#comment-1974 Comparing the sum and products of the elements does not work. Suppose a = [1, 9, 10] and b = [2, 3, 15].
PRODUCT [1, 9, 10] == 90
PRODUCT [2, 3, 15] == 90
SUM[2, 3, 15] == 20
SUM [1, 9, 10] == 20
[1, 9, 10] is NOT a permutation of [2, 3, 15]

]]>
By: Martin https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1973 Fri, 28 Aug 2015 11:30:55 +0000 http://www.ritambhara.in/?p=2852#comment-1973 Solution-1 works only if numbers in the array are unique. It returns true for {0, 1, 1} and {0, 0, 2}.

]]>
By: Kamal Rawat https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1972 Fri, 24 Jul 2015 02:42:55 +0000 http://www.ritambhara.in/?p=2852#comment-1972 Mehboob Elahi Thanks for pointing it out.. updated the post.

]]>
By: Mehboob Elahi https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1971 Wed, 22 Jul 2015 03:39:41 +0000 http://www.ritambhara.in/?p=2852#comment-1971 Kamal Rawat multiplication can case statck overflow or multiplication can results in very large no out of range of the variable

]]>
By: Kamal Rawat https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1970 Mon, 06 Jul 2015 08:09:54 +0000 http://www.ritambhara.in/?p=2852#comment-1970 you are right… Just curious, can you think of any example where the product is not zero?

]]>
By: Parth Sharma https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1969 Sat, 04 Jul 2015 05:23:39 +0000 http://www.ritambhara.in/?p=2852#comment-1969 Solution 2 will not work if a entry is 0 for ex 0,1,2,4 on multiplication give 0 and on adding will give 7 and if we have other array as 0,2,2,3 sum is 7 and product is also 0

]]>
By: shakul https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1968 Sat, 27 Jun 2015 16:53:42 +0000 http://www.ritambhara.in/?p=2852#comment-1968 Yeah! right if the array has duplicate elements than it will not work. I thought array has unique elements i.e. set not dups.

]]>
By: Kamal Rawat https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1967 Sat, 27 Jun 2015 15:40:15 +0000 http://www.ritambhara.in/?p=2852#comment-1967 In reply to shakul.

Thanks for the solution.. But, this will not work.. consider two arrays: {1, 1, 3} and {2, 2, 3}. The XOR of these two arrays will be zero

]]>
By: shakul https://demo.ritambhara.in/check-if-two-arrays-are-permutation-of-each-other/#comment-1966 Sat, 27 Jun 2015 15:26:36 +0000 http://www.ritambhara.in/?p=2852#comment-1966 Good Article Kamal!
Another way to solve this –
1. create XOR of elements of first set. Let the XOR value is S.
2. read element of second set one by one and XOR with S i.e. S=S XOR element of second array.
3. if set 1 is same as set 2 , S will become zero otherwise not.
complexity O(n)

]]>