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.
j
is both an int and an array (you refer to the "elements of j"), which makes your question hard to understand.