AfterAcademy Tech
•
01 Oct 2019

Difficulty Level : Medium
Asked in : Google, Facebook
Problem Description: Given two integer arrays A[] and B[] of size m and n, respectively. We need to find the intersection of these two arrays. The intersection of two arrays is a list of distinct numbers which are present in both the arrays. The numbers in the intersection can be in any order.
For example
Input: A[] = {1,4,3,2,5, 8,9} , B[] = {6,3,2,7,5}
Output: {3,2,5}
Input : A[] = {3,4,6,7,10, 12, 5}, B[] = {7,11,15, 18}
Output: {7}
Possible follow-up questions to ask the interviewer:-
We are discussing four ways to solve this problem :
For each element in A[], perform a linear search on B[]. Keep on adding the common elements to a list. To make sure the numbers in the list are unique, either check before adding new numbers to list or remove duplicates from the list after adding all numbers.
Pseudo-Code
int[] findIntersection(int A[], int B[], int m, int n)
{
int ans[]
for ( i = 0 to m-1 )
{
for ( j = 0 to n-1 )
{
if ( A[i] == B[j] )
if ( A[i] not in ans)
ans.add(A[i])
}
}
return ans
}
Complexity Analysis
There are two loops in the solution where the outer loop runs n times and the inner loop runs m times. So in the worst case, time complexity = O(m*n).
Space Complexity: worst-case = O(1) (Why? Some space is being consumed by answer list. Why isn't it taken into consideration?)
Critical ideas to think!
In the brute force approach, we select elements from array A[] and linearly search for them in array B[]. We could increase the efficiency of searching the elements if we sort B[] and apply binary search to search for the elements. Binary Search takes O(logn) time complexity.
Solution Steps
Pseudo-Code
bool binary_search(int B[], int n, int K)
{
int low = 0, high = n
while( low <= high )
{
int mid = low + ( high - low ) / 2
if( B[mid] == K )
return True
else if( B[mid] > K )
high = mid - 1
else
low = mid + 1
}
return false
}
int[] findIntersection(int A[], int B[], int m, int n)
{
int ans[]
sort(B,n)
for(i = 0 to m-1)
{
if(binary_search(B, n, A[i]))
ans.add(A[i])
}
return ans
}
Complexity Analysis
Time Complexity: Sorting array B[] + Searching for all elements of A[] in B[] = O(nlogn) + O(mlogn) = O(nlogn + mlogn)
Space Complexity: If we use merge sort, then space complexity is O(n), else if we use heap sort, space complexity is O(1).
Critical ideas to think!
If both A[] and B[] are sorted, we can use a two-pointer approach. At each step, we compare one element of A[] with one element of B[]. If both are equal, we add it to the list, else we move forward.
Solution Steps
5. Return answer list.
Solution Visualization

Pseudo-Code
int[] findIntersection(int A[], int B[], int m, int n)
{
int ans[]
sort(A, m)
sort(B, n)
i = 0, j = 0
while( i < m and j < n )
{
if ( A[i] == B[j] and A[i] not in ans)
{
ans.add(A[i])
i = i + 1
j = j + 1
}
else if ( A[i] < B[j] )
i = i + 1
else
j = j + 1
}
return ans
}
Complexity Analysis
Time Complexity : Sorting array A[] + Sorting array B[] + Using two pointer approach = O(mlogm) + O(nlogn) + O(n+m) = O(mlogm + nlogn)
Space Complexity: If mergeSort is used, then O(m), else in heapSort, O(1)
Critical ideas to think!
We will make a hash table of the smaller array so that our searching becomes faster and all elements appear only once. Now for each element of the bigger array, we will perform a search operation of that element in the hash table. The search operation in a hash table is, in an average case, O(1).
Solution Steps
Solution Visualization

Pseudo-Code
int findIntersection(int A[], int B[], int m, int n)
{
int ans[]
Take HashTable H of Size n
for(i = 0 to n-1)
{
H.insert(B[i])
}
for( i = 0 to m-1)
{
if(H.Search(A[i]) is true)
ans.add(A[i])
}
return ans
}
Complexity Analysis
Time Complexity = Inserting n elements of A[] in hash table + Time complexity of searching m elements of B[] in the hash table
= m* O(1) + n * O(1)) = O(m+n)
Space Complexity = O(n), for storing the auxiliary hash table
Critical ideas to think!

Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)

AfterAcademy Tech
Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times.

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.

AfterAcademy Tech
Given an array A[] of size n, you need to find the next greater element for each element in the array. Return an array that consists of the next greater element of A[i] at index i.
