<jupyter_start><jupyter_text>Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = target Note:Example:Given array nums = [-8, -7, 5, 2], target=0return (-7,5,2)Ex: (1,2,2,2) target =6 1) 6-1 = 5           1.1) 5-2=3    store the value of j in set {2}         1.2) 5-2=3    store the value of j in set {2}        1.3) 5-2=3    store the value of j in set {2} 2) 6-2 =4        2.1) 4-2= 2   This value exists in set , return<jupyter_code>#O(n3) . Here the time complexity is O n3
def three_sum(arr,target):
    for i in range(len(arr)):
        for j in range(i+1,len(arr)):
            for k in range(j+1,len(arr)):
                if arr[i]+arr[j]+arr[k] == target:
                    return arr[i],arr[j],arr[k]
print(three_sum([3,2,2,2],6))<jupyter_output>(2, 2, 2)<jupyter_text>Here the time complexity is  O (n2)<jupyter_code>def three_sum_sort(arr,target):
    #sort
    arr.sort
    for i in range(len(arr)-2):
        partial_target=target-arr[i]
        j=i+1
        k=len(arr)-1
        while j < k:
            partial_sum=arr[j]+arr[k]
            if partial_sum == partial_target:
                return (arr[i],arr[j],arr[k])
            elif partial_sum > target:
                k-=1
            else:
                j+=1
    return None
print(three_sum_sort([-8,-7,5,2],0))<jupyter_output>(-7, 5, 2)<jupyter_text>Here the time complexity is  O (n2)<jupyter_code># Python3 program to find a triplet using Hashing 
# returns true if there is triplet with sum equal 
# to 'sum' present in A[]. Also, prints the triplet 
def three_sum_hash(A,sum): 
    for i in range(0,len(A)-1): 
        #Find pair in subarray A[i+1..n-1]  
        # with sum equal to sum - A[i] 
        s = set() 
        curr_sum = sum - A[i] 
        for j in range(i+1,len(A)): 
            if (curr_sum - A[j]) in s: 
                print("Triplet is", A[i],  
                        ", ", A[j], ", ", curr_sum-A[j]) 
                return True
            s.add(A[j]) 
    return False
print(three_sum_hash([-8,-7,5,2],0))  
def three_sum(num,target):
    for i in range(0,len(num)-1):
        s=set()
        curr_sum = target - num[i]
        for j in range(i+1,len(num)):
            if(curr_sum- num[j]) in s:
                return(num[i],num[j],curr_sum-num[j])
            s.add(num[j])
    return False

print(three_sum([-8,-7,5,2],0)) 
def three_sum_n(arr,target):
    #sort the array
    arr.sort
    for i in range(len(arr)-2):
        print("i {}".format(i))
        print("target {}".format(target))
        print("arr[i] {}".format(arr[i]))
        partial_target =target-arr[i]
        print("partial_target {}".format(partial_target))
        j=i+1
        k=len(arr) - 1
        print ("k {}".format(k))
        while j < k:
            partial_sum = arr[j]+arr[k]
            print("arr[j] {}".format(arr[j]))
            print("arr[k] {}".format(arr[k]))
            print("partial_sum {}".format(partial_sum))
            print("arr[i] {}".format(arr[i]))
            if partial_sum == partial_target:
                return (arr[i],arr[j],arr[k])
            elif partial_sum > partial_target: # Make sum smaller
                k-=1
            else:
                j+=1
    return None

print(three_sum([3,5,1,1],5))

def three_sum(ls,target):
    s=set()
    cur_sum=0
    for i in range(0,len(ls)-1):
        cur_sum =target-ls[i]
        print("cur_sum",cur_sum)
        for j in range(i+1,len(ls)):
            print("inside cur_sum",cur_sum)
            print('i',i)
            print('j',j)
            print("cur_sum-ls[j] ",cur_sum-ls[j])
            if cur_sum-ls[j] in s:
                return ls[i],ls[j],cur_sum-ls[j]
            else:
                s.add(ls[j])
                print(s)
    return False
print(three_sum([3,2,2,2],6))<jupyter_output>cur_sum 3
inside cur_sum 3
i 0
j 1
cur_sum-ls[j]  1
{2}
inside cur_sum 3
i 0
j 2
cur_sum-ls[j]  1
{2}
inside cur_sum 3
i 0
j 3
cur_sum-ls[j]  1
{2}
cur_sum 4
inside cur_sum 4
i 1
j 2
cur_sum-ls[j]  2
(2, 2, 2)
{'fraction_non_alphanumeric': 0.15550595238095238, 'fraction_numerical': 0.03298611111111111, 'mean_word_length': 2.014200298953662, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '593387', 'license': 'MIT', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1512, 'n_words': 374, 'path': 'array_strings/ipynb/3_sum.ipynb', 'repo_name': 'PRkudupu/Algo-python', 'size': '4032'}