Find k pairs with smallest sums in two arrays | Set 2 | Java 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 ; see point 2 ; updates opposite to step 1 ; updates according to rule 1 ; see point 2 ; updates opposite to rule 1 ; see point 2 ; else update as necessary ( point 1 ) ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; Driver Code
import java . util . * ; class GFG { static class _pair { int first , second ; } ; static void printKPairs ( int a1 [ ] , int a2 [ ] , int size1 , int size2 , int k ) { if ( k > ( size2 * size1 ) ) { System . out . print ( "k pairs don't existNEW_LINE"); return ; } _pair _one = new _pair ( ) ; _pair _two = new _pair ( ) ; _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 ] ) { System . out . print ( " [ " + a1 [ _one . first ] + " , ▁ " + a2 [ _one . second ] + " ] ▁ " ) ; _one . second = ( _one . second + 1 ) % size2 ; if ( _one . second == 0 ) _one . first = ( _one . first + 1 ) % size1 ; _two . second = ( _two . second + 1 ) % size2 ; if ( _two . second == 0 ) _two . first = ( _two . first + 1 ) % size2 ; } else { System . out . print ( " [ " + a2 [ _one . second ] + " , ▁ " + a1 [ _one . first ] + " ] ▁ " ) ; _one . first = ( _one . first + 1 ) % size1 ; if ( _one . first == 0 ) _one . second = ( _one . second + 1 ) % size2 ; _two . first = ( _two . first + 1 ) % size2 ; if ( _two . first == 0 ) _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 ] ) { System . out . print ( " [ " + a1 [ _one . first ] + " , ▁ " + a2 [ _one . second ] + " ] ▁ " ) ; _one . second = ( ( _one . second + 1 ) % size2 ) ; if ( _one . second == 0 ) _one . first = ( _one . first + 1 ) % size1 ; } else { System . out . print ( " [ " + a2 [ _one . second ] + " , ▁ " + a1 [ _one . first ] + " ] ▁ " ) ; _one . first = ( ( _one . first + 1 ) % size1 ) ; if ( _one . first == 0 ) _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 ] ) { System . out . print ( " [ " + a2 [ _two . first ] + " , ▁ " + a1 [ _two . second ] + " ] ▁ " ) ; _two . first = ( ( _two . first + 1 ) % size2 ) ; if ( _two . first == 0 ) _two . second = ( _two . second + 1 ) % size1 ; } else { System . out . print ( " [ " + a1 [ _two . second ] + " , ▁ " + a2 [ _two . first ] + " ] ▁ " ) ; _two . second = ( ( _two . second + 1 ) % size1 ) ; if ( _two . second == 0 ) _two . first = ( _two . first + 1 ) % size1 ; } } cnt ++ ; } } public static void main ( String [ ] args ) { int a1 [ ] = { 2 , 3 , 4 } ; int a2 [ ] = { 1 , 6 , 5 , 8 } ; int size1 = a1 . length ; int size2 = a2 . length ; int k = 4 ; printKPairs ( a1 , a2 , size1 , size2 , k ) ; } }