Find two non | Function to find two non - overlapping with same Sum in an array ; first create an empty Map key -> which is Sum of a pair of elements in the array value -> vector storing index of every pair having that Sum ; consider every pair ( arr [ i ] , arr [ j ] ) and where ( j > i ) ; calculate Sum of current pair ; if Sum is already present in the Map ; check every pair having equal Sum ; if pairs don 't overlap,  print them and return ; Insert current pair into the Map ; If no such pair found ; Driver Code
def findPairs ( arr , size ) : NEW_LINE INDENT Map = { } NEW_LINE for i in range ( 0 , size - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT Sum = arr [ i ] + arr [ j ] NEW_LINE if Sum in Map : NEW_LINE INDENT for pair in Map [ Sum ] : NEW_LINE INDENT m , n = pair NEW_LINE if ( ( m != i and m != j ) and ( n != i and n != j ) ) : NEW_LINE INDENT print ( " Pair ▁ First ▁ ( { } , ▁ { } ) " . format ( arr [ i ] , arr [ j ] ) ) NEW_LINE print ( " Pair ▁ Second ▁ ( { } , ▁ { } ) " . format ( arr [ m ] , arr [ n ] ) ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if Sum not in Map : NEW_LINE INDENT Map [ Sum ] = [ ] NEW_LINE DEDENT Map [ Sum ] . append ( ( i , j ) ) NEW_LINE DEDENT DEDENT print ( " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 4 , 7 , 8 , 4 ] NEW_LINE size = len ( arr ) NEW_LINE findPairs ( arr , size ) NEW_LINE DEDENT