LeetCode – Find K Pairs with Smallest Sums (Java)

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.

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

Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.


Given nums1 = [1,7,11], nums2 = [2,4,6],  k = 3

Return: [1,2],[1,4],[1,6]

The first 3 pairs are returned from the sequence:

Java Solution

This problem is similar to Super Ugly Number. The basic idea is using an array to track the index of the next element in the other array.

The best way to understand this solution is using an example such as nums1={1,3,11} and nums2={2,4,8}.

public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
    List<int[]> result = new ArrayList<int[]>();
    k = Math.min(k, nums1.length*nums2.length);
        return result;
    int[] idx = new int[nums1.length];
        int min = Integer.MAX_VALUE;
        int t=0;
        for(int i=0; i<nums1.length; i++){
            if(idx[i]<nums2.length && nums1[i]+nums2[idx[i]]<min){
                min = nums1[i]+nums2[idx[i]];
        int[] arr = {nums1[t], nums2[idx[t]]};    
    return result;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";