Sudo Placement | Beautiful Pairs | Java code for finding required pairs ; The function to check if beautiful pair exists ; Set for hashing ; Traversing the first array ; Traversing the second array to check for every j corresponding to single i ; x + y = z = > x = y - z ; if such x exists then we return true ; hash to make use of it next time ; no pair exists ; Driver Code ; If pair exists then 1 else 0 2 nd argument as size of first array fourth argument as sizeof 2 nd array
import java . util . * ; class GFG { static boolean pairExists ( int [ ] arr1 , int m , int [ ] arr2 , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( s . contains ( arr2 [ j ] - arr1 [ i ] ) ) return true ; } s . add ( arr1 [ i ] ) ; } return false ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 5 , 10 , 8 } ; int [ ] arr2 = { 2 , 20 , 13 } ; if ( pairExists ( arr1 , 4 , arr2 , 3 ) ) System . out . println ( 1 ) ; else System . out . println ( 0 ) ; } }