Intersection of Three Sorted Arrays - Problem

Given three integer arrays arr1, arr2, and arr3 that are sorted in strictly increasing order, your task is to find the intersection of all three arrays.

The intersection contains only the integers that appear in all three arrays. Return these common elements as a sorted array.

For example, if arr1 = [1,2,3,4], arr2 = [2,3,4,5], and arr3 = [3,4,5,6], then the intersection would be [3,4] since only 3 and 4 appear in all three arrays.

Key Points:

  • All input arrays are already sorted in ascending order
  • Elements must appear in ALL THREE arrays to be included
  • Return result in sorted order (naturally sorted due to input constraints)

Input & Output

example_1.py โ€” Basic intersection
$ Input: arr1 = [1,2,3,4], arr2 = [2,3,4,5], arr3 = [3,4,5,6]
โ€บ Output: [3,4]
๐Ÿ’ก Note: Elements 3 and 4 appear in all three arrays. Element 1 only appears in arr1, element 2 appears in arr1 and arr2 but not arr3, elements 5 and 6 don't appear in all three arrays.
example_2.py โ€” No common elements
$ Input: arr1 = [1,2,3], arr2 = [4,5,6], arr3 = [7,8,9]
โ€บ Output: []
๐Ÿ’ก Note: No elements appear in all three arrays, so the intersection is empty.
example_3.py โ€” Single element intersection
$ Input: arr1 = [1,2,3,4,5], arr2 = [1,3,5,7,9], arr3 = [1,4,7,10,13]
โ€บ Output: [1]
๐Ÿ’ก Note: Only element 1 appears in all three arrays. Element 3 appears in arr1 and arr2 but not arr3, element 5 appears in arr1 and arr2 but not arr3, etc.

Visualization

Tap to expand
Three Dancers Finding Common RhythmEach dancer follows their sorted sequenceD1Sequence: 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5Currently at: 3D2Sequence: 2 โ†’ 3 โ†’ 4 โ†’ 5 โ†’ 6Currently at: 3D3Sequence: 3 โ†’ 4 โ†’ 5 โ†’ 6 โ†’ 7Currently at: 3๐ŸŽญ SYNCHRONIZED! ๐ŸŽญAll dancers at position 3Record this performance moment!
Understanding the Visualization
1
Start Position
All three dancers start at their first position
2
Find Synchronization
Check if all dancers are on the same beat
3
Advance Slower Dancers
Move dancers who are behind to catch up
4
Performance Moment
When synchronized, record the performance
5
Continue Dance
All dancers advance to next position
Key Takeaway
๐ŸŽฏ Key Insight: Like synchronized dancers, the three pointers move through sorted arrays, only "performing" (adding to result) when all three point to the same value!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

Three nested loops, each potentially iterating through all elements

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using a few variables, not counting output array

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค arr1.length, arr2.length, arr3.length โ‰ค 1000
  • 1 โ‰ค arr1[i], arr2[i], arr3[i] โ‰ค 2000
  • All arrays are sorted in strictly increasing order
  • No duplicate elements within each array
Asked in
Google 42 Facebook 38 Amazon 35 Microsoft 28
42.3K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen