DEV Community

Discussion on: Daily Challenge #63- Two Sum

Collapse
 
devparkk profile image
Dev Prakash

global sum_value
sum_value = int(input("Enter the sum value :"))
arr = [ int(i) for i in input("Enter array elements :" ).split( )]
def two_sum (arr) :
    for i in range(len(arr)) :
        for j in  range(i+1 , len(arr)) :
            if arr[j] + arr[i] == sum_value :
                return (i , j)
        return "NO SUCH PAIRS FOUND"

print (two_sum(arr))

Another one

global sum_value
sum_value = int(input("Enter the sum value :"))
arr = [ int(i) for i in input("Enter array elements :" ).split( )]
def two_sum (arr) :
    for i in range(len(arr)) :
        second_num = sum_value - arr[i]
        if second_num in arr :
            return (i , arr.index(second_num))
    return "NO SUCH PAIRS FOUND"
print (two_sum(arr))