Easy || Optimized || Efficient Intersection of Multiple Arrays using Sorting and Two Pointers
IntuitionTo find the intersection of multiple arrays, we need to identify the common elements that appear in every array.
The key insight here is that sorting e
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To find the intersection of multiple arrays, we need to identify the common elements that appear in every array.
The key insight here is that sorting each array allows us to efficiently find common elements using the two-pointer technique.
We can progressively reduce the problem size by iterating through each array and updating the intersection based on previously found common elements.
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Sort Each Array:**
    - Start by sorting all arrays in nums. Sorting ensures that elements are in ascending order, making it easier to find common elements across arrays using a two-pointer approach.
2. **Two-Pointer Technique:**
- Begin with the intersection being the first array (vec = nums[0]).
- For each subsequent array in nums, initialize a temporary result array (temp) and use two pointers (j and k) to traverse both the current array and the ongoing intersection (vec).
    - If the elements are equal, add it to the temporary result (temp) and move both pointers forward.
    - If the current element in the array is smaller, move the pointer for the array forward.
    - If the current element in the intersection is smaller, move the pointer for the intersection forward.
- After processing each array, update vec to the temp (intersection of the current array and the previous results).
3. **Final Intersection:**
- After processing all arrays, the result (vec) contains the common elements that appear in every array.
# Complexity
- **Time complexity:**
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
`O(N×MlogM), where 𝑁 is the number of arrays and 𝑀 is the maximum size of the arrays.`
- **Space complexity:** `O(N)`
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> intersection(vector<vector<int>>& nums) {
        int n = nums.size();
        vector<int> ans;
        for (auto& arr : nums) {
            sort(arr.begin(), arr.end());
        }
        vector<int> vec = nums[0];
        for (int i = 1; i < n; i++) {
            vector<int> temp;
            int j = 0;
            int k = 0;
            while (j < nums[i].size() && k < vec.size()) {
                if (nums[i][j] == vec[k]) {
                    temp.push_back(vec[k]);
                    j++;
                    k++;
                } else if (nums[i][j] > vec[k]) {
                    k++;
                } else {
                    j++;
                }
            }
            vec = temp;
        }
        ans = vec;
        return ans;
    }
};


```
['Array', 'Two Pointers', 'Sorting', 'C++']