# Triplet sum closest to given number

Problem Statement:

Given an array of integers, consisting of N integers denoted as arr[], and an integer X, the objective is to identify three integers within arr[] such that their sum closely approximates the value of X.

Sample Test Cases:

Test Case 1:

Input: arr[] = {-3, 5, 2, -8, 10}, X = 7

Output: 7

Explanation:

Sums of triplets:

(-3) + 5 + 2 = 4

5 + 2 + (-8) = -1

(-3) + 2 + (-8) = -9

4 is closest to 7.

Test Case 2:

Input: arr[] = {0, -6, 4, 9, -2}, X = 3

Output: 1

Explanation:

Sums of triplets:

0 + (-6) + 4 = -2

(-6) + 4 + 9 = 7

0 + 4 + (-2) = 2

1 is closest to 3.

Test Case 3:

Input:arr[] = {7, -1, 3, 6, -4}, X = 2

Output: 0

Explanation:

Sums of triplets:

7 + (-1) + 3 = 9

(-1) + 3 + 6 = 8

7 + 3 + (-4) = 6

0 is closest to 2.

Method 1: Naive approach

A straightforward method involves exhaustively exploring all subsets of size three within the given array. Throughout this process, the algorithm maintains a record of the variance between the target value, X, and the sum of each subset. The subset with the minimum difference between its sum and X is then identified and returned as the result.

Algorithm:

1. Implement a solution using three nested loops with counters i, j, and k.
2. The initial loop iterates from the start to the end, the second loop runs from i+1 to the end, and the third loop runs from j+1 to the end.
3. Within the loops, assess whether the difference between the sum of the elements at positions i, j, and k and the provided target sum is smaller than the current minimum difference.

If so, update the current minimum. Finally, output the closest sum obtained.

Java Implementation of the above mentioned approach

Output:

Complexity Analysis:

Time Complexity: O(n^3)

• For iteration of three for loops

Space Complexity: O(1)

• As there is no extra space used the space complexity remains constant

Method 2: Using Sorting

• Enhancing the algorithm's efficiency involves employing the Two Pointers technique after sorting the array.
• The optimized approach involves iterating through the array and locating the first element of the triplet.
• Subsequently, the Two Pointers algorithm is applied to identify the number closest to (x - array[i]). The closest sum is then updated accordingly.

Utilizing the Two Pointers algorithm results in a linear time complexity, making it a superior alternative to nested loops

Algorithm:

1. Arrange the provided array in ascending order.
2. Iterate through the array, designating arr[i] as the fixed first element of a potential triplet.
3. Subsequently, establish two pointers, one at i + 1 and the other at n - 1, where n is the size of the array. Assess the sum of the triplet formed by arr[i], arr[left], and arr[right].
4. If the sum is less than the target sum, increment the left pointer.
5. Conversely, if the sum is greater, decrement the right pointer to reduce the sum.
6. Continuously update the closest sum encountered thus far during this process.

Java Implementation of the above mentioned approach

Output:

Complexity Analysis:

Time Complexity: O(n^2)

Space Complexity: O(1)