<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p>

<p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p>

<p>Return <em>the</em> <code>k</code> <em>pairs</em> <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> <em>with the smallest sums</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,7,11], nums2 = [2,4,6], k = 3
<strong>Output:</strong> [[1,2],[1,4],[1,6]]
<strong>Explanation:</strong> The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,1,2], nums2 = [1,2,3], k = 2
<strong>Output:</strong> [[1,1],[1,1]]
<strong>Explanation:</strong> The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>nums1</code> and <code>nums2</code> both are sorted in <strong>non-decreasing order</strong>.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
	<li><code>k &lt;=&nbsp;nums1.length *&nbsp;nums2.length</code></li>
</ul>

#include <vector>   // For vector usage
#include <queue>    // For priority_queue usage
#include <map>      // For map usage
using namespace std;

class Solution {    // Define a Solution class
public:
    // Define the function that takes two vectors and an integer k
    vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        
        // Define a map to track visited pairs of indices (i, j)
        map<pair<int, int>, int> mp;
        
        // Define a vector of vectors to store the answer (the k smallest pairs)
        vector<vector<int>> ans;
        
        // Define a priority queue (min-heap) to store pairs of sums and indices
        priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
        
        // Push the initial smallest pair (nums1[0], nums2[0]) into the priority queue
        pq.push({nums1[0] + nums2[0], {0, 0}});
        
        // Mark this pair as visited in the map
        mp[{0, 0}] = 1;
        
        // While the priority queue is not empty and we haven't found k pairs
        while (!pq.empty() && k > 0) {
            // Decrement k since we will find one pair in this iteration
            k--;
            
            // Get the smallest pair from the heap (sum, (i, j))
            pair<int, pair<int, int>> t1 = pq.top();
            pair<int, int> t = t1.second;
            
            // Remove this pair from the heap
            pq.pop();
            
            // Prepare the result vector to add the current smallest pair
            vector<int> res;
            res.push_back(nums1[t.first]);  // Add nums1[i]
            res.push_back(nums2[t.second]); // Add nums2[j]
            
            // Add this pair to the result vector
            ans.push_back(res);
            
            // If the next index in nums1 (i + 1) is valid and not yet visited
            if (mp[{t.first + 1, t.second}] != 1 && t.first + 1 < nums1.size()) {
                mp[{t.first + 1, t.second}] = 1;  // Mark it as visited
                pq.push({nums1[t.first + 1] + nums2[t.second], {t.first + 1, t.second}});  // Push the new pair into the heap
            }
            
            // If the next index in nums2 (j + 1) is valid and not yet visited
            if (mp[{t.first, t.second + 1}] != 1 && t.second + 1 < nums2.size()) {
                mp[{t.first, t.second + 1}] = 1;  // Mark it as visited
                pq.push({nums1[t.first] + nums2[t.second + 1], {t.first, t.second + 1}});  // Push the new pair into the heap
            }
        }
        
        // Return the k smallest pairs
        return ans;
    }
};
150451