Check for a pair with given sum Python

PROGRAM TO FIND A PAIR IN GIVEN ARRAY WITH A GIVEN SUM



# function to check for the given sum
# in the array
def printPairs(arr, arr_size, sum):
     
    # Create an empty hash set
    s = set()
     
    for i in range(0, arr_size):
        temp = sum-arr[i]
        if (temp in s):
            print "Pair with given sum "+ str(sum) +
       " is (" + str(arr[i]) + ", " + str(temp) + ")"
        s.add(arr[i])
 
# driver code
A = [1, 4, 45, 6, 10, 8]
n = 16
printPairs(A, len(A), n)


OUTPUT
Pair with given sum 16 is (10, 6)

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java