Suppose two arrays are given , we need to find the common elements. case 1 : two arrays are sorted . case 2 : two arrays are unsorted . Answer : case 1 : Do a search as we just move through them as we would merge them. As they are sorted , while searching the element in the 2nd array with the element in the 1st array , we can store the index where it the 1st array element exceeds the 2nd array element. Such that the next search can begin at this stored index. case 2 : Hash both the arrays , wherever collision occurs its a common element.
No comments:
Post a Comment