Something like this:
public static List<int[]> findKLargestPairs(int[] nums1, int[] nums2, int k) {
List<int[]> result = new ArrayList<>();
if(nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0 || k <= 0) {
return result;
}
int idx1 = 0;
int idx2 = 0;
while(result.size() < k) {
result.add(new int[]{nums1[idx1], nums2[idx2]});
if(idx1 + 1 == nums1.length && idx2 + 1 == nums2.length) {
break;
}
if(idx1 + 1 < nums1.length && result.size() < k) {
result.add(new int[]{nums1[idx1 + 1], nums2[idx2]});
}
if(idx2 + 1 < nums2.length && result.size() < k) {
result.add(new int[]{nums1[idx1], nums2[idx2 + 1]});
}
if(idx1 + 1 < nums1.length) {
idx1++;
}
if(idx2 + 1 < nums2.length) {
idx2++;
}
}
return result;
}
Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Problem Challenge 1 - Grokking the Coding Interview: Patterns for Coding Questions
Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Problem Challenge 1 - Grokking the Coding Interview: Patterns for Coding Questions