Skip to content

Commit da8aa8f

Browse files
authored
Merge pull request #4091 from SadafKausar2025/madeChanges
Points Not added in the leaderboard
2 parents 7f6df6e + 60234cb commit da8aa8f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

dsa-solutions/lc-solutions/0000-0099/0015-3sum.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ tags:
66
- Hash Table
77
- Math
88
- String
9+
- python
10+
- javascript
11+
- java
12+
- c++
913
description: find the number of triplets with given sum .
1014
sidebar_position: 15
1115
---
@@ -16,7 +20,7 @@ Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[
1620

1721
Notice that the solution set must not contain duplicate triplets.
1822

19-
### Example 1
23+
### Example 1:
2024

2125
- **Input:** `nums = [-1,0,1,2,-1,-4]`
2226
- **Output:** `[[-1,-1,2],[-1,0,1]]`
@@ -27,13 +31,13 @@ Notice that the solution set must not contain duplicate triplets.
2731
- The distinct triplets are `[-1,0,1]` and `[-1,-1,2]`.
2832
- Notice that the order of the output and the order of the triplets does not matter.
2933

30-
### Example 2
34+
### Example 2:
3135

3236
- **Input:** `nums = [0,1,1]`
3337
- **Output:** `[]`
3438
- **Explanation:** The only possible triplet does not sum up to 0.
3539

36-
### Example 3
40+
### Example 3:
3741

3842
- **Input:** `nums = [0,0,0]`
3943
- **Output:** `[[0,0,0]]`
@@ -237,23 +241,23 @@ public:
237241
res = []
238242
```
239243

240-
2. **Sort the Input Array:**
244+
2. **Sort the Input Array :**
241245

242246
- Sort the input array `nums` in non-decreasing order.
243247

244248
```python
245249
nums.sort()
246250
```
247251

248-
3. **Iterate Through the Array:**
252+
3. **Iterate Through the Array :**
249253

250254
- Iterate through each element in the sorted array `nums`.
251255

252256
```python
253257
for i in range(len(nums)):
254258
```
255259

256-
4. **Skip Duplicate Elements:**
260+
4. **Skip Duplicate Elements :**
257261

258262
- Check if the current element is a duplicate of the previous element and skip it if it is.
259263

@@ -262,7 +266,7 @@ public:
262266
continue
263267
```
264268

265-
5. **Initialize Pointers:**
269+
5. **Initialize Pointers :**
266270

267271
- Initialize two pointers `j` and `k` to point to the elements next to the current element `i` and at the end of the array, respectively.
268272

@@ -271,23 +275,23 @@ public:
271275
k = len(nums) - 1
272276
```
273277

274-
6. **Two-Pointer Approach:**
278+
6. **Two-Pointer Approach :**
275279

276280
- Use a two-pointer approach with pointers `j` and `k` to find triplets whose sum equals zero.
277281

278282
```python
279283
while j < k:
280284
```
281285

282-
7. **Calculate Total:**
286+
7. **Calculate Total :**
283287

284288
- Calculate the total sum of the current triplet.
285289

286290
```python
287291
total = nums[i] + nums[j] + nums[k]
288292
```
289293

290-
8. **Adjust Pointers Based on Total:**
294+
8. **Adjust Pointers Based on Total :**
291295

292296
- If the total sum is greater than zero, decrement the `k` pointer to decrease the total sum.
293297

@@ -311,7 +315,7 @@ public:
311315
j += 1
312316
```
313317

314-
9. **Handle Duplicate Triplets:**
318+
9. **Handle Duplicate Triplets :**
315319

316320
- Increment the `j` pointer to skip any duplicate elements.
317321

@@ -320,12 +324,12 @@ public:
320324
j += 1
321325
```
322326

323-
10. **Return Result:**
327+
10. **Return Result :**
324328
- Return the list `res` containing all the unique triplets whose sum is zero.
325329
```python
326330
return res
327331
```
328332

329333
This algorithm efficiently finds all unique trip
330334

331-
lets in the given array `nums` whose sum equals zero using a two-pointer approach. It avoids duplicate triplets by skipping duplicate elements during traversal.
335+
lets in the given array `nums` whose sum equals zero using a two-pointer approach. It avoids duplicate triplets by skipping duplicate elements during traversal.

0 commit comments

Comments
 (0)