# Java 3ms beats 100% submission

• public class Solution {
static int minValueIndex = 0;

``````public static List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
if (nums1.length == 0 || nums2.length == 0 || k == 0) return new LinkedList<>();
if (k > nums1.length * nums2.length) k = nums1.length * nums2.length;

int[] store = new int[nums1.length];
for (int i = 0; i < store.length; i++) store[i] = 0;

List<int[]> result = new ArrayList<>(k);
int count = 0;
int index = 0;
while (count < k) {
if (store[index] >= nums2.length) index++;

minValueIndex = index;
solveIter(nums1, nums2, store, index, nums1[index] + nums2[store[index]]);

store[minValueIndex] = store[minValueIndex] + 1;
count++;
}
return result;
}

static int solveIter(int[] nums1, int[] nums2, int[] store, int index, int min) {
if (min > nums1[index] + nums2[store[index]]) {
min = nums1[index] + nums2[store[index]];
minValueIndex = index;
}
if (store[index] > 0 && index < store.length - 1) {
min = solveIter(nums1, nums2, store, index + 1, min);
}

return min;
}
``````

}

• Not working for this input:

``````num1 => [1,1,2]
num2 => [1,2,3]
k => 2
``````

• @everlasting.puneet
the output is [1,1] [1,1] , is it not correct?

When I checked earlier, it didn't accept the solution. Might be because of `static` variable. It works perfectly fine now.