
Sponsored
Sponsored
This approach employs sorting the array and fixing two pointers while searching for the other two via a two-pointer method. This reduces the dimensionality of the problem stepwise.
Time Complexity: O(n^3), where n is the number of elements in the array due to three nested loops.
Space Complexity: O(1), excluding the space required for the output storage.
1#include <stdio.h>
2#include <stdlib.h>
3
4void swap(int *a, int *b) {
5 int temp = *a;
6 *a = *b;
7 *b = temp;
8}
9
10void sort(int *arr, int n) {
11 for (int i = 0; i < n - 1; i++) {
12 for (int j = 0; j < n - i - 1; j++) {
13 if (arr[j] > arr[j + 1]) {
14 swap(&arr[j], &arr[j + 1]);
15 }
16 }
17 }
18}
19
20int **fourSum(int *nums, int numsSize, int target, int *returnSize, int **returnColumnSizes) {
21 sort(nums, numsSize);
22 int **result = (int **)malloc(sizeof(int *) * 1000);
23 *returnSize = 0;
24 *returnColumnSizes = (int *)malloc(sizeof(int) * 1000);
25
26 for (int i = 0; i < numsSize - 3; i++) {
27 if (i > 0 && nums[i] == nums[i - 1]) continue;
28 for (int j = i + 1; j < numsSize - 2; j++) {
29 if (j > i + 1 && nums[j] == nums[j - 1]) continue;
30 int k = j + 1, l = numsSize - 1;
31 while (k < l) {
32 int sum = nums[i] + nums[j] + nums[k] + nums[l];
33 if (sum == target) {
34 result[*returnSize] = (int *)malloc(sizeof(int) * 4);
35 result[*returnSize][0] = nums[i];
36 result[*returnSize][1] = nums[j];
37 result[*returnSize][2] = nums[k];
38 result[*returnSize][3] = nums[l];
39 (*returnColumnSizes)[*returnSize] = 4;
40 (*returnSize)++;
41 while (k < l && nums[k] == nums[k + 1]) k++;
42 while (k < l && nums[l] == nums[l - 1]) l--;
43 k++; l--;
44 } else if (sum < target) {
45 k++;
46 } else {
47 l--;
48 }
49 }
50 }
51 }
52 return result;
53}
54
55int main() {
56 int nums[] = {1, 0, -1, 0, -2, 2};
57 int numsSize = sizeof(nums) / sizeof(int);
58 int target = 0;
59 int returnSize;
60 int *returnColumnSizes;
61 int **result = fourSum(nums, numsSize, target, &returnSize, &returnColumnSizes);
62 for (int i = 0; i < returnSize; i++) {
63 printf("[%d, %d, %d, %d]\n", result[i][0], result[i][1], result[i][2], result[i][3]);
64 free(result[i]);
65 }
66 free(result);
67 free(returnColumnSizes);
68 return 0;
69}In this C solution, a simple bubble sort algorithm is used to sort the array. The quadruplets are found by applying two nested loops fixing two elements and then using the two-pointer technique on the remaining array to find pairs that sum up to the required target. Duplication is avoided by skipping identical elements during iteration.
This method reduces the four-sum problem by first reducing it to a three-sum problem, and then a two-sum problem using hash maps.
Time Complexity: O(n^2), considering the use of a hash map.
Space Complexity: O(n), for storing intermediate and potential pairs.
1import
The Java algorithm effectively uses hash maps for reference pair evaluation, boosting performance by swift lookup and insertion. Sorting helps maintain increment of processing to ignore unwanted duplicate efforts.