Google Interview Question
Software Engineer / Developershow could it be O(n)...even if the arrays are sorted .The worst case time complexity is going to O(n2)
To get O(N) with a sorted array you could go through the arrays side by side.
If one of the array's current element is greater than the other array's current element, increment the array with the smaller value until it's current value reaches or exceeds that of the first array. Each time you increment, you can determine if the current element is in both arrays. Do this until reach the end of the smaller array.
The complexity would have been O(N) - N being the size of the smaller array.
Worst case would come if you had an unsorted array - you would sort (O(logN)) then do the above (O(N)) bringing the worst case to O(NlogN).
K2G, your solution wouldn't work for unsorted arrays.
Another approach: (independent of the order of Arrays - works for both sorted/unsorted arrays)
create a Binary Search Tree from Array A.
Now, search each element from array B. ( This is the best way to find duplicates)
BST creation - O(n)
searching m elements in a tree with n elements ( mlg n)
overall time complexity O(n + m lg n) -> m lg n
There are faster ways for...
From:)
http://forums.devarticles.com/c-c-help-52/how-to-compute-set-intersection-efficiently-59122.html
-----------
If duplicates are counted as one i.e. {1 2 3 3} = {1 2 3}:
1.) Assign a bit value for each item.
2.) Form two bit strings.
3.) Perform a bitwise & operation.
4.) Convert back into original.
e.g. {tom **** harry} n {harry harry fred}
Key: tom => 1 X 10^0
**** => 1 X 10^1
harry => 1 X 10^2
fred => 1 X 10^3
==> 0111 & 1100 => 0100
=> {harry}
Give credit or discredit to the original poster.
Code:
Assumption: Given 2 sorted arrays a, b of length len1 and len2 respectively
int m=0, n=0;
while(m < len1 && n < len2)
{
if(a[m] == b[n])
{
printf("%d ", a[m]);
m++;
n++;
}
else if(a[m] > b[n])
{
n++;
}
else
{
m++;
}
}
Iteration is over the minimum of lengths of 2 arrays, so complexity is O(min(m,n))
"Iteration is over the minimum of lengths of 2 arrays, so complexity is O(min(m,n))"
The above statement is not correct. Let's say, length of array A is 1 whose value is a big number like 1 billion and the length of array B is 1 million whose last element's value is 10 million. In this case you have to iterate over all the elements in array B!!!
i, j = 0, 0
a = [-9, -8, 0, 1, 4, 6]
b = [-8, -7, -6, 0, 1, 3, 5, 6]
while (i < a.size && j < b.size) do
if a[i] == b[j]
#puts "a[i] = #{a[i]} and b[j] = #{b[j]} Incrementing i and j"
puts a[i]
i += 1
j += 1
elsif a[i] < b[j]
#puts "a[i] = #{a[i]} and b[j] = #{b[j]} Incrementing j"
i += 1
elsif b[j] < a[i]
#puts "a[i] = #{a[i]} and b[j] = #{b[j]} Incrementing i"
j += 1
end
end
One of the best algorithms for finding the intersection of two arrays when the arrays are sorted is applying binary search to find all the elements of the other array in the first array and print the elements which are common in both the arrays:
Imlemenatation:
bool binary-search(int arr[], int low, int high, int key){
if(high >= low){
int mid = low + (high - low) / 2;
if(arr[mid] == key)
return true;
if(key > arr[mid])
return binary-search(arr, mid + 1, high, key);
else
return binary-search(arr, low, mid - 1, key);
}
return false;
}
int main{
for(int i = 0; i < m; i++){
if(binary-search(arr_1, 0, n, arr_2[i]) == true)
cout<<arr_2[i]<<" ";
}
Time complexity will be O(logn).
Space complexity will be O(1).
And while the array is not sorted then we can use a hashset to find the common elements.
Given: Two arrays A[1...n] and B[1...m]
Case 1: Two arrays sorted Solution is O(m+n) - Linear
- Nachiketha May 08, 2009