Hey guys as you've read in the question i am trying to find the element pairs in an array equal to the given sum and return the sum of their respective indices.
I was able to return the element pairs for the given sum but failed to return the sum of their indices. Here is my code:
arr = [1, 4, 2, 3, 0 , 5]
sum = 7
x = min(arr)
y = max(arr)
while x < y:
if x + y > sum:
y -= 1
elif x + y < sum:
x += 1
else:
print("(", x, y, ")")
x += 1
My output:
( 2 5 )
( 3 4 )
This is what i need to do further:
2 + 5 = 7 → Indices 2 + 5 = 7;
3 + 4 = 7 → Indices 3 + 1 = 4;
7 + 4 = 11 → Return 11;
Thanks in Advance!