Advertisement

3Sum Closest - LeetCode 16 Solution

3Sum Closest - Complete Solution Guide

3Sum Closest is LeetCode problem 16, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given 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 Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0). C

Detailed Explanation

The problem requires finding three numbers within a given array whose sum is closest to a specified target value. The input is an array of integers `nums` and a target integer `target`. The output is the sum of three integers from `nums` that has the minimum absolute difference with the `target`. We are guaranteed that there will be one unique answer.

Solution Approach

The solution uses a combination of sorting and the two-pointer technique. First, the input array is sorted. Then, the algorithm iterates through the array, fixing one number at a time. For each fixed number, two pointers are used to find a pair of numbers in the remaining part of the array such that their sum, combined with the fixed number, is closest to the target. The algorithm keeps track of the closest sum found so far and updates it whenever a closer sum is encountered.

Step-by-Step Algorithm

  1. Step 1: Sort the input array `nums` in ascending order.
  2. Step 2: Initialize `closest_sum` to the sum of the first three elements of the sorted array. This serves as the initial closest sum.
  3. Step 3: Iterate through the array from index `i = 0` to `n - 2` (where `n` is the length of the array).
  4. Step 4: For each index `i`, set `left = i + 1` and `right = n - 1`. These are the two pointers.
  5. Step 5: While `left < right`, calculate the `current_sum` as `nums[i] + nums[left] + nums[right]`.
  6. Step 6: If `current_sum` is equal to the `target`, return the `target` because it's the perfect match.
  7. Step 7: If the absolute difference between `current_sum` and `target` is less than the absolute difference between `closest_sum` and `target`, update `closest_sum` to `current_sum`.
  8. Step 8: If `current_sum` is less than `target`, increment `left` to consider a larger number.
  9. Step 9: If `current_sum` is greater than `target`, decrement `right` to consider a smaller number.
  10. Step 10: Repeat steps 5-9 until `left` and `right` meet.
  11. Step 11: After the outer loop completes, return the `closest_sum`.

Key Insights

  • Insight 1: Sorting the array allows us to use a two-pointer approach, significantly improving time complexity.
  • Insight 2: The two-pointer technique efficiently explores all possible pairs of numbers for each fixed number in the sorted array.
  • Insight 3: Keeping track of the `closest_sum` and updating it whenever a sum closer to the target is found is crucial.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Sorting.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Google, Meta, Microsoft, Uber, Yahoo, Zoho, tcs.