Problem of the day
You are given three arrays 'A', 'B' and 'C' of length 'N', 'M' and 'K' respectively. All the three arrays are sorted in non-decreasing order. Your task is to find all such elements which are present in all the three given arrays.
Note:
1. The output array should have the same ordering of elements as the original arrays.
2. Even if a particular element appears more than once in each of the three arrays, it should still be present only once in the output array.
3. If there are no common elements in the arrays, return an empty array.
For example:
Consider the three arrays A = [ 2, 3, 4, 7 ] , B = [ 0, 0, 3, 5 ] , C = [ 1, 3, 8, 9 ]
The output array should be [ 3 ] as 3 is the only element which is present in all the three arrays.
Input Format:
The first line of the input contains an integer 'T', denoting the number of test cases.
The first line of each test case contains three space-separated integers 'N', 'M' and 'K' denoting the number of elements in Array A, Array B and Array C respectively.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'A'.
The third line of each test case contains 'M' space-separated integers denoting the elements of the array 'B'.
The fourth line of each test case contains 'K' space-separated integers denoting the elements of the array 'C'.
Output Format:
The only line of output of each test case should contain the elements that are common in all three arrays separated by space.
Print the output of each test case in a new line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N, M, K <= 30000
0 <= A[i] <= 10^9
0 <= B[i] <= 10^9
0 <= C[i] <= 10^9
Where 'A[i]', 'B[i]', 'C[i]' denotes the 'i'th' element of the arrays 'A', 'B' and 'C' respectively.
Time Limit: 1 sec
2
4 3 3
1 4 5 6
3 4 5
5 6 7
3 1 3
4 6 8
6
2 2 6
5
6
For the first test case :
Elements that are common in array A and B = [ 4, 5 ]. Out of which only 5 is present in Array C. Therefore the output array is [ 5 ] in this case.
For the second test case :
It can be seen that only 6 is present in all the three arrays. Therefore the output array is [ 6 ] in this case.
2
4 4 3
1 2 2 3
2 2 3 3
2 2 3
3 2 2
1 2 3
0 6
4 6
2 3
For the first test case, elements 2,3 are present in all three arrays.
For the second test case, no element is common in all three arrays, hence we do not print anything.
Iterate through one array and for each element try to check whether it is present in the other two arrays.
The idea is to iterate through one of the three arrays and for each element of the array iterate through the other two arrays and check whether that element is present in the other two arrays. If that element exists in the other two arrays, then we will add it to the output array provided it already does not exist in the output array. At the end we will return the output array.
Note that we can arbitrarily select any of the three arrays as the first array. We are selecting Array A as the first array.
Steps :
O(N*(M+K)), where N, M and K are the number of elements in Arrays A, B and C respectively.
In the worst case, when none of the elements of Array A are present in both Array B and Array C we will need to execute (M+K) iterations for each of the N elements of Array A. Hence, the overall Time Complexity is O(N*(M+K)).
O(1), as we are using constant extra memory.