Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the quadruplet that is close to target using C#?
The quadruplet closest to target problem involves finding four numbers in an array whose sum is closest to a given target value. This is solved using the Two Pointers pattern, similar to the quadruplet sum to zero approach.
The algorithm works by sorting the array first, then using nested loops to fix the first two elements, and applying the two-pointer technique to find the optimal third and fourth elements. At each step, we track the difference between the current quadruplet sum and the target, keeping the closest sum found so far.
Algorithm Steps
-
Sort the array to enable the two-pointer approach.
-
Use two nested loops to fix the first two elements of the quadruplet.
-
Apply two pointers for the remaining two elements, moving them based on the sum comparison with the target.
-
Track the closest sum by comparing the absolute difference with the target.
Time and Space Complexity
Time Complexity: Sorting the array takes O(N log N). The main algorithm uses three nested loops, resulting in O(N³). Overall complexity is O(N³).
Space Complexity: O(N) for sorting the array, assuming the sorting algorithm uses additional space.
Example
using System;
using System.Linq;
public class FourSumClosest {
public int FindFourSumClosest(int[] nums, int target) {
if (nums == null || nums.Length < 4) {
return -1;
}
Array.Sort(nums);
int closestSum = nums[0] + nums[1] + nums[2] + nums[3];
for (int i = 0; i < nums.Length - 3; i++) {
for (int j = i + 1; j < nums.Length - 2; j++) {
int left = j + 1;
int right = nums.Length - 1;
while (left < right) {
int currentSum = nums[i] + nums[j] + nums[left] + nums[right];
if (Math.Abs(target - currentSum) < Math.Abs(target - closestSum)) {
closestSum = currentSum;
}
if (currentSum == target) {
return currentSum;
} else if (currentSum < target) {
left++;
} else {
right--;
}
}
}
}
return closestSum;
}
}
class Program {
public static void Main(string[] args) {
FourSumClosest solution = new FourSumClosest();
int[] nums = { 1, 0, -1, 0, -2, 2 };
int target = 0;
int result = solution.FindFourSumClosest(nums, target);
Console.WriteLine("Closest sum to target " + target + " is: " + result);
// Test with another example
int[] nums2 = { 4, 0, 5, -5, 3, 3, 0, -4, -5 };
int target2 = -2;
int result2 = solution.FindFourSumClosest(nums2, target2);
Console.WriteLine("Closest sum to target " + target2 + " is: " + result2);
}
}
The output of the above code is −
Closest sum to target 0 is: 0 Closest sum to target -2 is: -2
How It Works
The algorithm fixes two elements using nested loops, then uses two pointers to efficiently search for the remaining two elements. By comparing the absolute difference between each quadruplet sum and the target, we maintain the closest sum found so far.
When the current sum is less than the target, we move the left pointer right to increase the sum. When it's greater, we move the right pointer left to decrease the sum. This approach ensures we explore all possible combinations efficiently.
Conclusion
The quadruplet closest to target problem is efficiently solved using a combination of sorting and the two-pointer technique. With O(N³) time complexity, this approach provides an optimal solution for finding four numbers whose sum is closest to a given target value.
