Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

find a pair of elements from an array whose sum equals a given number python

#Given an array of integers and a number, write a function that finds two elements from the array whose sum is equal to the given number.
def find_pair(arr, num):
  for i in range(len(arr)):
    for j in range(i + 1, len(arr)):
      if arr[i] + arr[j] == num:
        return [arr[i], arr[j]]

print(find_pair([3, 6, 8, -8, 10, 8], 16))
[6, 10]
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #find #pair #elements #array #sum #equals #number #python
ADD COMMENT
Topic
Name
9+7 =