Intersection of Multiple Arrays - Problem

Imagine you're organizing a group project where each team member has their own list of preferred meeting days. You need to find the days that everyone is available to meet.

Given a 2D integer array nums where each nums[i] represents a non-empty array of distinct positive integers, your task is to find all integers that appear in every single array. The result should be returned as a list sorted in ascending order.

Think of it as finding the common elements across multiple sets - only the numbers that show up in all arrays make it to the final answer.

Example: If you have [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]], only [3,4] appear in all three arrays.

Input & Output

example_1.py — Python
$ Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]
Output: [3,4]
💡 Note: The numbers 3 and 4 are the only ones that appear in all three arrays. 1 and 2 are missing from the third array, 5 is missing from the second array, and 6 only appears in the third array.
example_2.py — Python
$ Input: nums = [[1,2,3],[4,5,6]]
Output: []
💡 Note: There are no common elements between the two arrays, so the intersection is empty.
example_3.py — Python
$ Input: nums = [[1,2,3,4,5]]
Output: [1,2,3,4,5]
💡 Note: When there's only one array, all elements in that array form the intersection by default.

Visualization

Tap to expand
📅 Team Meeting SchedulerAliceAvailable: 3,1,2,4,5BobAvailable: 1,2,3,4CarolAvailable: 3,4,5,6Finding Common AvailabilityStep 1: Start with Alice's schedule → {1, 2, 3, 4, 5}Step 2: Intersect with Bob's → {1, 2, 3, 4, 5} ∩ {1, 2, 3, 4} = {1, 2, 3, 4}Step 3: Intersect with Carol's → {1, 2, 3, 4} ∩ {3, 4, 5, 6} = {3, 4}Result: Everyone can meet on days 3 and 4! 🎉💡 Key InsightProgressive intersection efficiently narrows down possibilitiesThis is exactly how the optimal algorithm works with arrays!
Understanding the Visualization
1
Collect Schedules
Each team member provides their list of available meeting days
2
Find Overlap
Start with first person's availability, then narrow down by checking each subsequent person
3
Final Schedule
The remaining days are when everyone is available
Key Takeaway
🎯 Key Insight: By starting with one set and progressively intersecting with others, we efficiently find the common elements while minimizing memory usage and computation time.

Time & Space Complexity

Time Complexity
⏱️
O(n × m)

Visit each element once across all arrays, with set intersection operations being efficient

n
2n
Linear Growth
Space Complexity
O(min(m1, m2, ..., mn))

Set size never exceeds the smallest array size due to progressive intersection

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ sum(nums[i].length) ≤ 1000
  • 1 ≤ nums[i][j] ≤ 1000
  • All values in nums[i] are distinct
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
42.8K Views
Medium Frequency
~18 min Avg. Time
1.5K 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