Interview Question
Country: United States
For the second option i.e.
If extra space is not available and arrays can be changed, sort arrays and then print elements from A not in B. Complexity - O(n logn)
We can sort the arrays, but how do u print values which are not present in A, but not present in B? Whats the logic here?
“If extra space is available, use hash map to store elements from B.”
HashMap? key or value?
I'd like to change the wrong idea rooted deeply in your mind, I think you intended to use hashset, not hash map, and you think that if you use hash set, then the complexity will decreased, no, the implementation of hash set is including rb-tree, and the search time complexity of rb-tree is O(lgn), so the time complexity is also O(n*lgn). There is no algorithm of O(n) at all. And using hash set just can save your time and bring you convenience.
If we are allowed to use extra space:
1. Load items from array B into a collection (ex: ArrayList).
2. Loop through items in array A and in the loop check each item if it exists in the array list of B. If not found, print the item.
Efficiency = O(n), where n is number of items in array A.
Suggestions?
Sort the smaller array - B
and then perform binary search for each element of array A
print not found numbers.
Let size of A is m and B is n
1) Put all values of B in std::set() . O(nlogn)
2) Check all values of A in the set() if its there dot contains() method O(1)
if its there call remove() i.e. O(1)
we have to check for all m elements so O(m).
now print all the elements of this set.
overall : O(nlogn)+O(n).= O(nlogn) it can be done
Solution using sorting. Sort array b mlogm then for each element in a do a binary search nlogm, so total mlogm + nlogm
import java.util.ArrayList;
import java.util.Arrays;
public class ArrIntersection {
public static ArrayList<Integer> ANotB(int[] a,int[] b){
ArrayList<Integer> c = new ArrayList<Integer>();
Arrays.sort(b);
for (int i=0; i<a.length; ++i){
int idx = Arrays.binarySearch(b, a[i]);
if (idx < 0){
c.add(a[i]);
}
}
return c;
}
public static void main(String[] args){
int[] a = {1,4,3,6,9};
int[] b = {2,8,4,3};
ArrayList<Integer> c = ANotB(a,b);
for (Integer num : c){
System.out.println(num);
}
}
}
Solution with Hash, map b to hash O(m), search every item in a in the hash O(n), so total o(n)+o(m).
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ArrIntersection {
public static ArrayList<Integer> ANotBWithHash(int[] a,int[] b){
Map<Integer, Boolean> bMap = new HashMap<Integer, Boolean>();
for (int i=0; i<b.length; ++i){
bMap.put(b[i], true);
}
ArrayList<Integer> c = new ArrayList<Integer>();
for (int i=0; i<a.length; ++i){
if (!bMap.containsKey(a[i])){
c.add(a[i]);
}
}
return c;
}
public static void main(String[] args){
int[] a = {1,4,3,6,9};
int[] b = {2,8,4,3};
ArrayList<Integer> c = ANotBWithHash(a, b);
for (Integer num : c){
System.out.println(num);
}
}
}
If extra space is available, use hash map to store elements from B. Complexity - O(n)
- Epic January 24, 2013If extra space is not available and arrays can be changed, sort arrays and then print elements from A not in B. Complexity - O(n logn)
If extra space is not available and arrays cannot be changed, use two loops to check if element of A is present in B. Complexity - O(n2).