Find four elements that sum to a given value | Set 2 | A hashing based Java program to find if there are four elements with given sum . ; The function finds four elements with given sum X ; Store sums of all pairs in a hash table ; Traverse through all pairs and search for X - ( current pair sum ) . ; If X - sum is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver Code ; Function call
import java . util . HashMap ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findFourElements ( int arr [ ] , int n , int X ) { HashMap < Integer , pair > mp = new HashMap < Integer , pair > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) mp . put ( arr [ i ] + arr [ j ] , new pair ( i , j ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( mp . containsKey ( X - sum ) ) { pair p = mp . get ( X - sum ) ; if ( p . first != i && p . first != j && p . second != i && p . second != j ) { System . out . print ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ p . first ] + " , ▁ " + arr [ p . second ] ) ; return ; } } } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = arr . length ; int X = 91 ; findFourElements ( arr , n , X ) ; } }