Program to Find the two Elements such that their Sum is Closest to Zero

a=list(map(int,input().split()))
l=0
r=1
minsum=a[0]+a[1]
for i in range(len(a)):
    for j in range(i+1,len(a)):
        s=a[i]+a[j]
        if(abs(minsum)>abs(s)):
            minsum=s
            l=i
            r=j
print("the two elements are ",a[l],a[r])
    


Input 1 2 6 4 8 10 
 output the two elements are 1 2

No comments:

Post a Comment