Pregunta de entrevista de Amazon

Find Common elements from two same sized Integer Arrays(Unsorted) in O(n) without sorting either of them.

Respuestas de entrevistas

Anónimo

18 sep 2010

Hash 1 Array 1 Look up for each element in Array 2 Append to common element array if found. O(N)

5

Anónimo

24 ago 2010

That is N*N complexity, cleverly disguised as single loop. Resetting i every time it reaches N and incrementing j does it.

4

Anónimo

18 ago 2010

#include main() { //take arrays of three elements for comparision int a[3]={1,2,3}; int b[3]={1,4,2}; int i,j; j=0; for(i=0;i<3;i++) { if(a[i]==b[j]) { printf("common elements are:%d\n",a[i]); } if(i==2&j!=2) { i=0; j++; } } }

1