Given three integer arrays arr1, arr2 and arr3sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
Example 1:
Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
Output: [1,5]
Explanation: Only 1 and 5 appeared in the three arrays.
The three arrays are sorted, length at most \(1000\), and values lie in \([1,2000]\). Counting all three, a value with count \(3\) is common; elements are unique inside each array, so one array cannot inflate the count. Emitting in \(arr1\) order keeps the result sorted.
Traverse the three arrays, count the occurrence of each number, then traverse any one of the arrays. If the count of a number is \(3\), add it to the result array.
The time complexity is \(O(n)\), and the space complexity is \(O(m)\). Here, \(n\) and \(m\) are the length of the array and the range of numbers in the array, respectively.
Counting needs an array proportional to the value range. The arrays are already sorted, so we binary-search each \(arr1\) value in \(arr2\) and \(arr3\). Extra space becomes constant; time becomes \(O(n\log n)\).
Traverse the first array. For each number, use binary search to find this number in the second and third arrays. If found in both, add this number to the result array.
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(1)\). Here, \(n\) is the length of the array.