1.

Write a program to check and return the pairs of a given array A whose sum value is equal to a target value N.

Answer»

This can be done easily by using the PHENOMENON of hashing. We can use a hash map to check for the current value of the ARRAY, X. If the map has the value of (N-x), then there is our pair.

def print_pairs(arr, N): # hash set hash_set = set() for i in range(0, len(arr)): val = N-arr[i] if (val in hash_set): #check if N-x is there in set, print the pair print("Pairs " + str(arr[i]) + ", " + str(val)) hash_set.add(arr[i])# driver codearr = [1, 2, 40, 3, 9, 4]N = 3print_pairs(arr, N)


Discussion

No Comment Found