Repository navigation
Expand file tree
/
Copy pathoptimized.cpp
More file actions
71 lines (60 loc) · 2.58 KB
/
Copy pathoptimized.cpp
File metadata and controls
71 lines (60 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
/*
* 3Sum - Optimized Solution using Two Pointers Technique
*
* Approach:
* - Sort the array to enable two-pointer technique and duplicate skipping
* - Fix one element (nums[i]) and use two pointers for remaining elements
* - Use left pointer from i+1 and right pointer from end
* - Skip duplicates at all three positions to avoid duplicate triplets
* - Early termination when nums[i] > 0 since array is sorted
*
* Time Complexity: O(n²) - Outer loop O(n), inner two-pointer scan O(n)
* Space Complexity: O(1) - Only using constant extra space (excluding output)
*/
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
const int n = nums.size();
// Sort array to enable two-pointer technique and duplicate detection
sort(nums.begin(), nums.end());
// Fix first element and find pairs that sum to -nums[i]
for (int i = 0; i < n - 2; ++i) {
// Early termination: if first element > 0, no valid triplet possible
if (nums[i] > 0) break;
// Skip duplicates for first element
if (i > 0 && nums[i] == nums[i - 1]) continue;
// Two-pointer technique for remaining elements
int left = i + 1;
int right = n - 1;
while (left < right) {
const int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
// Found valid triplet
result.emplace_back(vector<int>{nums[i], nums[left], nums[right]});
// Skip duplicates for second element
while (left < right && nums[left] == nums[left + 1]) {
++left;
}
// Skip duplicates for third element
while (left < right && nums[right] == nums[right - 1]) {
--right;
}
// Move both pointers
++left;
--right;
} else if (sum < 0) {
// Sum too small, move left pointer to increase sum
++left;
} else {
// Sum too large, move right pointer to decrease sum
--right;
}
}
}
return result;
}
};