Problem Description
Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
Key Insights
- Since all arrays are sorted in strictly increasing order, we can efficiently use three pointers.
- By comparing the current elements pointed to in each array, determine if the element is common.
- Advance the pointer(s) corresponding to the smallest element when elements differ.
- The approach avoids extra space for hash structures and does a linear scan through the arrays.
Space and Time Complexity
Time Complexity: O(n1 + n2 + n3), where n1, n2, n3 are the lengths of arr1, arr2, and arr3 respectively. Space Complexity: O(1) extra space (excluding the output array).
Solution
We use three pointers (one for each array) to traverse the arrays concurrently:
- If the elements at all three pointers are equal, add that element to the result list and move all three pointers forward.
- If they are not equal, move the pointer that points to the smallest element.
- Continue until one of the arrays is completely traversed.
This technique leverages the sorted property of the arrays to ensure that no element is missed and maintains a linear time complexity.