5

We need to check if 2 arrays are similar or not. The elements can be duplicate as well. For example A = {2,3,4,5,6,6} and B = {3,6,2,4,6,5} are similar.

I have a naive solution :

foreach i:int in arr1
 foreach j:int in arr2
  {
    if(i == j)
     j = -1;
  } 

Now if all the elements of j are -1 , then we can say that the 2 arrays are similar. Can someone give a test case in which this won't work (i hope it should work though!) ?

Also this is O(n^2). Can we do better ? Sorting and Hashing are not allowed.

12
  • 3
    Why do you list 3 languages? Perhaps your looking for language-agnostic?
    – Joe
    Commented Mar 7, 2012 at 15:58
  • Do you have to estimate or to be sure? Commented Mar 7, 2012 at 15:59
  • Not homework ! Relisted to just algorithm
    – h4ck3d
    Commented Mar 7, 2012 at 15:59
  • I think j is both an int and an array (you refer to the "elements of j"), which makes your question hard to understand. Commented Mar 7, 2012 at 16:01
  • @Cygal What i am basically doing is that updating elements of B whenever a match is found , if all elements are matching then all the elements will be -1 , in that case the arrays will be similar. Hope you get it now.
    – h4ck3d
    Commented Mar 7, 2012 at 16:03

4 Answers 4

5

You can do it in O(max(LA, LB)) time, where LA and LB are lengths of A and B, respectively, but at the price of using O(M) space, where M is an allowed range of values in the arrays (i.e. there are such constants min and max, so that min <= a, b <= max holds true for every a in A and b in B).

seen = array[min..max]
foreach a in A
    seen[a] = 'a'

foreach b in B
    if seen[b] != 'a'
        // A didn't contain b
        return "A and B are not equivalent"
    else
       seen[b] = 'a,b'

foreach s in seen
    if s == 'a'
        // A did contain a which was not in B
        return "A and B are not equivalent"

return "A and B are equivalent"

This approach is practical if the arrays are very large, but all their values fit in a small range.

5
  • 1
    @Heinzi: you are absolutely right, this is a bucket sort in disguise :) Commented Mar 7, 2012 at 16:46
  • 1
    @IgorKorkhov it works ,but like i mentioned sorting and hashing are not allowed
    – h4ck3d
    Commented Mar 7, 2012 at 17:07
  • @NiteeshMehra: well, strictly speaking it is not sorting since it does not keep track of a number of elements seen so far. Bucket sort counts the number of elements in each bucket in order to return sorted sequence at its final stage, while my algorithm doesn't. Having said that, I agree that my algorithm is a half-baked bucket sort. Commented Mar 7, 2012 at 17:17
  • 1
    @NiteeshMehra: just curious, if it's not a homework or a brain-teaser, why is the restriction? Commented Mar 7, 2012 at 17:25
  • @IgorKorkhov This is equivalent to hashing !
    – h4ck3d
    Commented Mar 11, 2012 at 16:43
3

You can use a binary search tree that you build from one of them. Now go over the other one and check if the value is already in the binary search tree. This one runs in O(nlgn) and use O(n) space.

5
  • length of both array has to be same for them to be similar.
    – h4ck3d
    Commented Mar 9, 2012 at 14:28
  • Great! It's not hashing nor sorting. The implementation must handle non-unique values but it's not hard and the complexities stay the same. Commented Mar 9, 2012 at 21:10
  • 2
    @AviCohen , generating the BST == sorting isn't it?
    – h4ck3d
    Commented Mar 11, 2012 at 9:35
  • 2
    @DavidCosta It is equivalent to sorting. Generating a BST. Then for searching we need in order traversal which is == sorting.
    – h4ck3d
    Commented Mar 11, 2012 at 9:40
  • @NiteeshMehra I didn't think of the solution as a sort, indeed it is. Commented Mar 13, 2012 at 2:18
0

A = {2,3,4, 5,-1,6} and B = {3,6,2,4,6,5}

you should add break in your if-statement, without it your code will work only is there is no duplicates

8
  • After iteration B = {-1,-1,-1,-1,-1,5} , so they are not similar !
    – h4ck3d
    Commented Mar 7, 2012 at 16:01
  • @NiteeshMehra oh, there must be A = {2,3,4, 5, -1,6} and B = {3,6,2,4,6,5}, sorry =)
    – Alecs
    Commented Mar 7, 2012 at 16:04
  • @NiteeshMehra what do you mean by "the same"? B = {-1,-1,-1,-1,-1,5}? It can't be
    – Alecs
    Commented Mar 7, 2012 at 16:09
  • @NiteeshMehra: Well, probably Alecs meant B = {2,3,4, 5, -1,6} and A = {3,6,2,4,6,5}. Commented Mar 7, 2012 at 16:09
  • breaking is also wrong, you will always skip the second encounter of the duplicate in B, since j will never reach it and fulfil i==j, because you terminated the run when you found the first element in B.
    – amit
    Commented Mar 7, 2012 at 16:11
0

i just want to mention that u don't have to run your algorithm(whether it has a complexity of o(n*m) or max(n,m)) for all input combinations.Run your algorithm only if xor of all the elements of both arrays is equal to zero. i.e a[0] xor a[1] xor...b[0] xor...b[n]=0. otherwise u can surely say that array A and B is not equal.

1
  • That is xor of all elements of a should be equal to xor of all elements of b ?
    – h4ck3d
    Commented Mar 8, 2012 at 5:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.