Find k pairs with smallest sums in two arrays | Set 2 | C ++ program to print the k smallest pairs | Set 2 ; Function to print the K smallest pairs ; if k is greater than total pairs ; _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 [ ] ; Repeat the above process till all K pairs are printed ; when both the pointers are pointing to the same elements ( point 3 ) ; updates according to step 1 ; if ( _one . second == 0 ) see point 2 ; updates opposite to step 1 ; updates according to rule 1 ; if ( _one . first == 0 ) see point 2 ; updates opposite to rule 1 ; if ( _two . first == 0 ) see point 2 ; else update as necessary ( point 1 ) ; updating according to rule 1 ; if ( _one . second == 0 ) see point 2 ; updating according to rule 1 ; if ( _one . first == 0 ) see point 2 ; updating according to rule 1 ; if ( _two . first == 0 ) see point 2 ; updating according to rule 1 ; if ( _two . second == 0 ) see point 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef struct _pair { int first , second ; } _pair ; void printKPairs ( int a1 [ ] , int a2 [ ] , int size1 , int size2 , int k ) { if ( k > ( size2 * size1 ) ) { cout << " k ▁ pairs ▁ don ' t ▁ exist STRNEWLINE " ; return ; } _pair _one , _two ; _one . first = _one . second = _two . first = _two . second = 0 ; int cnt = 0 ; while ( cnt < k ) { if ( _one . first == _two . second && _two . first == _one . second ) { if ( a1 [ _one . first ] < a2 [ _one . second ] ) { cout << " [ " << a1 [ _one . first ] << " , ▁ " << a2 [ _one . second ] << " ] ▁ " ; _one . second = ( _one . second + 1 ) % size2 ; _one . first = ( _one . first + 1 ) % size1 ; _two . second = ( _two . second + 1 ) % size2 ; if ( _two . second == 0 ) _two . first = ( _two . first + 1 ) % size2 ; } else { cout << " [ " << a2 [ _one . second ] << " , ▁ " << a1 [ _one . first ] << " ] ▁ " ; _one . first = ( _one . first + 1 ) % size1 ; _one . second = ( _one . second + 1 ) % size2 ; _two . first = ( _two . first + 1 ) % size2 ; _two . second = ( _two . second + 1 ) % size1 ; } } 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 ] << " ] ▁ " ; _one . second = ( ( _one . second + 1 ) % size2 ) ; _one . first = ( _one . first + 1 ) % size1 ; } else { cout << " [ " << a2 [ _one . second ] << " , ▁ " << a1 [ _one . first ] << " ] ▁ " ; _one . first = ( ( _one . first + 1 ) % size1 ) ; _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 ] << " ] ▁ " ; _two . first = ( ( _two . first + 1 ) % size2 ) ; _two . second = ( _two . second + 1 ) % size1 ; } else { cout << " [ " << a1 [ _two . second ] << " , ▁ " << a2 [ _two . first ] << " ] ▁ " ; _two . second = ( ( _two . second + 1 ) % size1 ) ; _two . first = ( _two . first + 1 ) % size1 ; } } cnt ++ ; } } 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 ; }