<jupyter_start><jupyter_text>3Sum ClosestGiven an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.Return the sum of the three integers.You may assume that each input would have exactly one solution. Example 1:- Input: nums = [-1,2,1,-4], target = 1- Output: 2- Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). Example 2:- Input: nums = [0,0,0], target = 1- Output: 0  Constraints:3 <= nums.length <= 1000-1000 <= nums[i] <= 1000-104 <= target <= 104 Solution IntuitionSort the array. Loop through all elements and during each iteration loop the sub-array that is after the current element of the main loop.  Solution<jupyter_code>def three_sum_closest(nums: list, target: int) -> int:
    nums.sort()
    distance = None
        
    for i in range(len(nums)):
        j, k = i + 1, len(nums) - 1
        
        while j < k:
            tmp = nums[i] + nums[j] + nums[k]
            tmp_dist = abs(tmp - target)
            if distance is None or distance[0] > tmp_dist:
                distance = (tmp_dist, tmp) 
                  
            if tmp > target:
                k -= 1
            elif tmp < target:
                j += 1
            else:
                k, j = k - 1, j + 1
        
    return distance[1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09955752212389381, 'fraction_numerical': 0.03392330383480826, 'mean_word_length': 2.084090909090909, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 8, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '581849', 'license': 'MIT', 'n_tokens_mistral': 448, 'n_tokens_neox': 403, 'n_words': 165, 'path': 'python-data-structures/leetocde/3-sum-closest.ipynb', 'repo_name': 'dimastatz/courses', 'size': '1356'}