2 or 3 Numbers Leetcode
How to solve the famous problem of 2 or 3 Numbers?

2 Number sum
Finding a function that takes in input an array of distinct integers and a target and as output returns all couple inside the array whose sum is the target. So, if we sum all the numbers inside the couple found, we will get the target sum.
#Solution 1 time Complexity O(N²) with duplication
array = [1,2,3,4,-1,-2,5]
target = 4
response=[]
length=len(array)
for i in range(length):
for j in range(length):
myTotal = array[i]+array[j]
if(myTotal == target and i!=j):
response.append([array[i],array[j]])
print(response)
#Solution 2 time Complexity O(N²) without duplication
array = [1,2,3,4,-1,-2,5]
target = 4
response=[]
length=len(array)
for i in range(length):
for j in range(i+1,length):
myTotal = array[i]+array[j]
if(myTotal == target):
response.append([array[i],array[j]])
print(response)
#Solution 3 time Complexity O(N*log(N)) without duplication
array = [1,2,3,4,-1,-2,5]
target = 4
response=[]
length=len(array)
left=0
right=length-1
array.sort()
while left < right:
myTotal = array[left]+array[right]
if(myTotal < target):
left = left +1
elif(myTotal > target):
right = right -1
elif(myTotal == target):
response.append([array[left],array[right]])
right = right -1
left = left +1
3 Number sum
Same solution using python Pandas
##### 3 Number Sum
array = [1,2,3,4,-1,-2,5]
target = 4
response=[]
length=len(array)
array.sort()
for i in range(length):
left=i+1
right = length-1
while left < right :
myTotal = array[i]+array[left]+array[right]
if(myTotal < target):
left = left +1
elif(myTotal > target):
right = right -1
elif(myTotal == target):
response.append([array[i],array[left],array[right]])
right = right -1
left = left +1