Skip to content

Commit 856e13f

Browse files
committed
question-added
1 parent 4fd1606 commit 856e13f

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

Three-Sum/Approach-to-solve.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<h1><a href = "https://leetcode.com/problems/3sum/description/" target="_blank">Three Sum</a><h1>
2+
<h3>This repository contains a C++ solution to the Leetcode Three Sum problem using the optimal two-pointer approach. The Three Sum problem is a classic algorithmic problem where the goal is to find all unique triplets in an array that sum up to zero.</h3>
3+
<h2>Problem Statement</h2>
4+
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
5+
6+
Notice that the solution set must not contain duplicate triplets.
7+
8+
<h2>Approach: Optimal Two Pointer Approach for Three Sum Problem</h2>
9+
The two-pointer approach is an efficient way to solve the Three Sum problem. Here's a breakdown of the approach used in the code:
10+
<ul>
11+
<li><h3>Sorting:</h3>
12+
<ul>
13+
<li>The first step involves sorting the input array nums. Sorting the array allows us to use the two-pointer technique efficiently.</li>
14+
</ul>
15+
</li>
16+
<li><h3>Iterating Through the Array::</h3>
17+
<ul>
18+
<li>We iterate through the sorted array using a for loop.</li>
19+
<li>Inside the loop, we handle duplicates by checking if the current element is the same as the previous element. If it is, we skip to the next element to avoid duplicate triplets.</li>
20+
</ul>
21+
</li>
22+
<li><h3>Two Pointers:</h3>
23+
<ul>
24+
<li>For each element nums[i], we initialize two pointers, j and k, where j points to i + 1, and k points to the last element of the array.</li>
25+
<li>We use these two pointers to traverse the array inward to find pairs that sum up to the negation of the current element (-nums[i]).</li>
26+
</ul>
27+
</li>
28+
<li><h3>Finding Triplets:</h3>
29+
<ul>
30+
<li>Within the two-pointer loop, we calculate the sum of the current triplet (nums[i] + nums[j] + nums[k]).</li>
31+
<li>If the sum is greater than zero, we decrement k to decrease the sum.</li>
32+
<li>If the sum is less than zero, we increment j to increase the sum.</li>
33+
<li>If the sum equals zero, we found a triplet. We add this triplet to the ans vector.</li>
34+
<li>After finding a triplet, we handle duplicates by skipping identical elements for both j and k.</li>
35+
</ul>
36+
</li>
37+
<li><h3>Returning the Result:</h3>
38+
<ul>
39+
<li>Finally, we return the vector ans containing all unique triplets that sum up to zero.</li>
40+
</ul>
41+
42+
<h3>Pseudocode: </h3>
43+
44+
function threeSum(nums):
45+
// Sort the input array
46+
sort(nums)
47+
48+
// Initialize an empty list to store triplets
49+
ans = []
50+
51+
// Iterate through the sorted array
52+
for i from 0 to length(nums) - 1:
53+
// Skip duplicates
54+
if i > 0 AND nums[i] == nums[i-1]:
55+
continue
56+
57+
// Initialize two pointers, j and k
58+
j = i + 1
59+
k = length(nums) - 1
60+
61+
// Two Pointer Technique
62+
while j < k:
63+
// Calculate the sum of current triplet
64+
sum = nums[i] + nums[j] + nums[k]
65+
66+
// If sum is greater than zero, decrement k
67+
if sum > 0:
68+
k = k - 1
69+
// If sum is less than zero, increment j
70+
else if sum < 0:
71+
j = j + 1
72+
// If sum is zero, found a triplet
73+
else:
74+
// Add the triplet to the list
75+
ans.append([nums[i], nums[j], nums[k]])
76+
77+
// Handle duplicates for j and k
78+
while j < k AND nums[j] == nums[j + 1]:
79+
j = j + 1
80+
while j < k AND nums[k] == nums[k - 1]:
81+
k = k - 1
82+
83+
// Move j and k pointers
84+
j = j + 1
85+
k = k - 1
86+
87+
// Return the list of triplets
88+
return ans
89+
90+
<h3>Cpp Code:</h3>
91+
92+
vector<vector<int>>ans;
93+
sort(nums.begin(),nums.end());
94+
for(int i = 0 ;i < nums.size();i++){
95+
96+
if(i > 0 && nums[i] == nums[i-1]){
97+
continue;
98+
}
99+
100+
int j = i + 1;
101+
int k = nums.size()-1;
102+
while(j<k){
103+
int sum = nums[i] + nums[j]+nums[k];
104+
if(sum > 0){
105+
k--;
106+
}
107+
else if(sum < 0){
108+
j++;
109+
}
110+
else{
111+
vector<int>temp = {nums[i],nums[j],nums[k]};
112+
ans.push_back(temp);
113+
j++,k--;
114+
while(j < k && nums[j] == nums[j-1]) {
115+
j++;
116+
}
117+
while(j<k && nums[k] == nums[k+1]){
118+
k--;
119+
}
120+
}
121+
122+
}
123+
}
124+
return ans;
125+
126+
\*\*(For full code , refer to the .cpp file)
127+
128+
<h2>Complexity Analysis</h2>
129+
<ul>
130+
<h3>Time Complexity: 𝑂(nlog n) + O(n^2) where n is size of array.</h3>
131+
132+
<h3>Space Complexity: O(number of triplets)</h3>
133+
134+
</ul>
135+
136+
<h2>Contributing:</h2>
137+
Contributions to improve the efficiency or readability of the code are welcome. Feel free to submit pull requests with your enhancements.

Three-Sum/Question.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<h1> <a href = "https://leetcode.com/problems/3sum/description/" target = "_blank">1. Three Sum</a></h1>
2+
<h4>
3+
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
4+
5+
Notice that the solution set must not contain duplicate triplets.
6+
7+
</h4>
8+
<h5>
9+
Example 1:
10+
Input: nums = [-1,0,1,2,-1,-4]
11+
<br>
12+
Output: [[-1,-1,2],[-1,0,1]]
13+
<br><br>
14+
Explanation:
15+
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
16+
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
17+
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
18+
The distinct triplets are [-1,0,1] and [-1,-1,2].
19+
Notice that the order of the output and the order of the triplets does not matter.
20+
<br><br>
21+
Example 2:
22+
23+
Input: nums = [0,1,1] <br>
24+
Output: []<br>
25+
Explanation: The only possible triplet does not sum up to 0.
26+
27+
<br>
28+
Example 3:
29+
30+
Input: nums = [0,0,0]<br>
31+
Output: [[0,0,0]]<br>
32+
Explanation: The only possible triplet sums up to 0.
33+
34+
</h5>
35+
<p>
36+
Constraints:
37+
38+
3 <= nums.length <= 3000
39+
-105 <= nums[i] <= 105
40+
41+
</p>

Three-Sum/three-sum.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution
2+
{
3+
public:
4+
vector<vector<int>> threeSum(vector<int> &nums)
5+
{
6+
7+
// OPTIMAL APPROACH : TWO POINTER APPROACH:
8+
9+
vector<vector<int>> ans;
10+
sort(nums.begin(), nums.end());
11+
for (int i = 0; i < nums.size(); i++)
12+
{
13+
14+
if (i > 0 && nums[i] == nums[i - 1])
15+
{
16+
continue;
17+
}
18+
19+
int j = i + 1;
20+
int k = nums.size() - 1;
21+
while (j < k)
22+
{
23+
int sum = nums[i] + nums[j] + nums[k];
24+
if (sum > 0)
25+
{
26+
k--;
27+
}
28+
else if (sum < 0)
29+
{
30+
j++;
31+
}
32+
else
33+
{
34+
vector<int> temp = {nums[i], nums[j], nums[k]};
35+
ans.push_back(temp);
36+
j++, k--;
37+
while (j < k && nums[j] == nums[j - 1])
38+
{
39+
j++;
40+
}
41+
while (j < k && nums[k] == nums[k + 1])
42+
{
43+
k--;
44+
}
45+
}
46+
}
47+
}
48+
return ans;
49+
}
50+
};

0 commit comments

Comments
 (0)