|
| 1 | +/* Name : Abhinav kumar |
| 2 | +Github username : Abhinavcode13 |
| 3 | +Repository name : data-structures-and-algorithms |
| 4 | +Problem : Add Three number sum in C++ |
| 5 | +Issue Number : #242 |
| 6 | +Problem statement : 3Sum |
| 7 | +
|
| 8 | +Explanation of the below cpp code : |
| 9 | +
|
| 10 | +The code first sorts the input array in ascending order. Then, it iterates through the array and uses the two-pointer technique to find the remaining two elements that add up to the negative of the current element. |
| 11 | +
|
| 12 | +We use two pointers, one at the beginning and one at the end of the remaining array after fixing the current element. If the sum of the three elements is less than zero, we increase the left pointer to increase the sum. If the sum is greater than zero, we decrease the right pointer to decrease the sum. If the sum is zero, we add the triplet to the result vector, and skip duplicate elements by increasing the left pointer and decreasing the right pointer until they point to different elements. |
| 13 | +
|
| 14 | +The time complexity of the code is O(n^2), where n is the length of the input array. The sorting step takes O(n log n) time, and the nested loop takes O(n^2) time in the worst case. However, we skip many iterations of the loop using the two-pointer technique, making the actual running time faster than the worst-case time complexity. |
| 15 | +*/ |
| 16 | +-----------------------------------------------------------------------------------------------------------//C++ code begins here------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +#include <bits/stdc++.h> |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +vector<vector<int>> threeSum(vector<int>& nums) { |
| 22 | + vector<vector<int>> result; |
| 23 | + int n = nums.size(); |
| 24 | + sort(nums.begin(), nums.end()); // Sort the input array |
| 25 | + |
| 26 | + // Fix the first element and use two pointer technique to find the remaining two elements |
| 27 | + for (int i = 0; i < n - 2; i++) { |
| 28 | + if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicate elements |
| 29 | + |
| 30 | + int l = i + 1, r = n - 1; |
| 31 | + while (l < r) { |
| 32 | + int sum = nums[i] + nums[l] + nums[r]; |
| 33 | + if (sum < 0) { |
| 34 | + l++; // Increase left pointer to increase the sum |
| 35 | + } else if (sum > 0) { |
| 36 | + r--; // Decrease right pointer to decrease the sum |
| 37 | + } else { |
| 38 | + result.push_back({nums[i], nums[l], nums[r]}); |
| 39 | + // Skip duplicate elements |
| 40 | + while (l < r && nums[l] == nums[l + 1]) l++; |
| 41 | + while (l < r && nums[r] == nums[r - 1]) r--; |
| 42 | + l++; |
| 43 | + r--; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +int main() { |
| 51 | + vector<int> nums = {-1, 0, 1, 2, -1, -4}; |
| 52 | + vector<vector<int>> result = threeSum(nums); |
| 53 | + for (vector<int> triplet : result) { |
| 54 | + for (int x : triplet) { |
| 55 | + cout << x << " "; |
| 56 | + } |
| 57 | + cout << endl; |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
0 commit comments