# Find k pairs with smallest sums in two arrays | Set 2

Given two arrays arr1[] and arr2[] sorted in ascending order and an integer K. The task is to find k pairs with smallest sums such that one element of a pair belongs to arr1[] and another element belongs to arr2[]. Sizes of arrays may be different. Assume all the elements to be distinct in each array.

**Examples:**

Input: a1[] = {1, 7, 11} a2[] = {2, 4, 6} k = 3 Output: [1, 2], [1, 4], [1, 6] 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]. Input: a1[] = { 2, 3, 4 } a2[] = { 1, 6, 5, 8 } k = 4 Output: [1, 2] [1, 3] [1, 4] [2, 6]

An approach with time complexity O(k*n1) has been discussed here.

**Efficient Approach: **Since the array is already sorted. The given below algorithm can be followed to solve this problem:

- The idea is to maintain two pointers, one pointer pointing to one pair in (a1, a2) and other in (a2, a1). Each time, compare the sums of the elements pointed by the two pairs and print the minimum one. After this, increment the pointer to the element in the printed pair which was larger than the other. This helps to get the next possible k smallest pair.
- Once the pointer has been updated to the element such that it starts pointing to the first element of the array again, update the other pointer to the next value. This update is done cyclically.
- Also, when both the pairs are pointing to the same element, update pointers in both the pairs so as to avoid extra pair’s printing. Update one pair’s pointer according to rule1 and other’s opposite to rule1. This is done to ensure that all the permutations are considered and no repetitions of pairs are there.

Below is the working of the algorithm for example 1:

a1[] = {1, 7, 11}, a2[] = {2, 4}, k = 3

Let the pairs of pointers be _one, _two

_one.first points to 1, _one.second points to 2 ;

_two.first points to 2, _two.second points to 1

1st pair:

Since _one and _two are pointing to same elements, print the pair once and update

- print [1, 2]
then update _one.first to 1, _one.second to 4 (following rule 1) ;

_two.first points to 2, _two.second points to 7 (opposite to rule 1).

If rule 1 was followed for both, then both of them would have been pointing to 1 and 4,

and it is not possible to get all possible permutations.

2nd pair:

Since a1[_one.first] + a2[_one.second] < a1[_two.second] + a2[_two.first], print them and update

- print [1, 4]
then update _one.first to 1, _one.second to 2

Since _one.second came to the first element of the array once again,

therefore _one.first points to 7Repeat the above process for remaining K pairs

Below is the C++ implementation of the above approach:

`// C++ program to print the k smallest ` `// pairs | Set 2 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `struct` `_pair { ` ` ` `int` `first, second; ` `} _pair; ` ` ` `// Functio to print the K smallest pairs ` `void` `printKPairs(` `int` `a1[], ` `int` `a2[], ` ` ` `int` `size1, ` `int` `size2, ` `int` `k) ` `{ ` ` ` ` ` `// if k is greater than total pairs ` ` ` `if` `(k > (size2 * size1)) { ` ` ` `cout << ` `"k pairs don't exist\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// _pair _one keeps track of ` ` ` `// 'first' in a1 and 'second' in a2 ` ` ` `// in _two, _two.first keeps track of ` ` ` `// element in the a2[] and _two.second in a1[] ` ` ` `_pair _one, _two; ` ` ` `_one.first = _one.second = _two.first = _two.second = 0; ` ` ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Repeat the above process till ` ` ` `// all K pairs are printed ` ` ` `while` `(cnt < k) { ` ` ` ` ` `// when both the pointers are pointing ` ` ` `// to the same elements (point 3) ` ` ` `if` `(_one.first == _two.second ` ` ` `&& _two.first == _one.second) { ` ` ` `if` `(a1[_one.first] < a2[_one.second]) { ` ` ` `cout << ` `"["` `<< a1[_one.first] ` ` ` `<< ` `", "` `<< a2[_one.second] << ` `"] "` `; ` ` ` ` ` `// updates according to step 1 ` ` ` `_one.second = (_one.second + 1) % size2; ` ` ` `if` `(_one.second == 0) ` `// see point 2 ` ` ` `_one.first = (_one.first + 1) % size1; ` ` ` ` ` `// updates opposite to step 1 ` ` ` `_two.second = (_two.second + 1) % size2; ` ` ` `if` `(_two.second == 0) ` ` ` `_two.first = (_two.first + 1) % size2; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"["` `<< a2[_one.second] ` ` ` `<< ` `", "` `<< a1[_one.first] << ` `"] "` `; ` ` ` ` ` `// updates according to rule 1 ` ` ` `_one.first = (_one.first + 1) % size1; ` ` ` `if` `(_one.first == 0) ` `// see point 2 ` ` ` `_one.second = (_one.second + 1) % size2; ` ` ` ` ` `// updates opposite to rule 1 ` ` ` `_two.first = (_two.first + 1) % size2; ` ` ` `if` `(_two.first == 0) ` `// see point 2 ` ` ` `_two.second = (_two.second + 1) % size1; ` ` ` `} ` ` ` `} ` ` ` `// else update as necessary (point 1) ` ` ` `else` `if` `(a1[_one.first] + a2[_one.second] ` ` ` `<= a2[_two.first] + a1[_two.second]) { ` ` ` `if` `(a1[_one.first] < a2[_one.second]) { ` ` ` `cout << ` `"["` `<< a1[_one.first] << ` `", "` ` ` `<< a2[_one.second] << ` `"] "` `; ` ` ` ` ` `// updating according to rule 1 ` ` ` `_one.second = ((_one.second + 1) % size2); ` ` ` `if` `(_one.second == 0) ` `// see point 2 ` ` ` `_one.first = (_one.first + 1) % size1; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"["` `<< a2[_one.second] << ` `", "` ` ` `<< a1[_one.first] << ` `"] "` `; ` ` ` ` ` `// updating according to rule 1 ` ` ` `_one.first = ((_one.first + 1) % size1); ` ` ` `if` `(_one.first == 0) ` `// see point 2 ` ` ` `_one.second = (_one.second + 1) % size2; ` ` ` `} ` ` ` `} ` ` ` `else` `if` `(a1[_one.first] + a2[_one.second] ` ` ` `> a2[_two.first] + a1[_two.second]) { ` ` ` `if` `(a2[_two.first] < a1[_two.second]) { ` ` ` `cout << ` `"["` `<< a2[_two.first] << ` `", "` `<< a1[_two.second] << ` `"] "` `; ` ` ` ` ` `// updating according to rule 1 ` ` ` `_two.first = ((_two.first + 1) % size2); ` ` ` `if` `(_two.first == 0) ` `// see point 2 ` ` ` `_two.second = (_two.second + 1) % size1; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"["` `<< a1[_two.second] ` ` ` `<< ` `", "` `<< a2[_two.first] << ` `"] "` `; ` ` ` ` ` `// updating according to rule 1 ` ` ` `_two.second = ((_two.second + 1) % size1); ` ` ` `if` `(_two.second == 0) ` `// see point 2 ` ` ` `_two.first = (_two.first + 1) % size1; ` ` ` `} ` ` ` `} ` ` ` `cnt++; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `a1[] = { 2, 3, 4 }; ` ` ` `int` `a2[] = { 1, 6, 5, 8 }; ` ` ` `int` `size1 = ` `sizeof` `(a1) / ` `sizeof` `(a1[0]); ` ` ` `int` `size2 = ` `sizeof` `(a2) / ` `sizeof` `(a2[0]); ` ` ` `int` `k = 4; ` ` ` `printKPairs(a1, a2, size1, size2, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

[1, 2] [1, 3] [1, 4] [2, 6]

**Time complexity**: O(K)

## Recommended Posts:

- Smallest Integer to be inserted to have equal sums
- Find if a binary matrix exists with given row and column sums
- Minimum sum of absolute difference of pairs of two arrays
- Find smallest number K such that K % p = 0 and q % K = 0
- Find the number of pairs (a, b) such that a % b = K
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find frequency of smallest value in an array
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Find the k smallest numbers after deleting given elements
- Find the Side of the smallest Square that can contain given 4 Big Squares
- Find Nth smallest number that is divisible by 100 exactly K times
- Find the Kth smallest element in the sorted generated array
- Find the lexicographically smallest string which satisfies the given condition
- Find k ordered pairs in array with minimum difference d
- Find the smallest positive number missing from an unsorted array | Set 3

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.