Skip to content

Commit f28aa74

Browse files
committed
resolved and write in better way for dsa solution
1 parent 3280673 commit f28aa74

File tree

1 file changed

+61
-25
lines changed

1 file changed

+61
-25
lines changed

dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ description: "This is a solution to the Two Sum problem on LeetCode."
1515

1616
## Problem Description
1717

18-
|Problem Statement | Solution Link | LeetCode Profile|
19-
|:---|:---|:---|
18+
| Problem Statement | Solution Link | LeetCode Profile |
19+
| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- |
2020
| [Two Sum on LeetCode](https://leetcode.com/problems/two-sum/) | [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions) | [Ajay Dhangar](https://leetcode.com/ajaydhangar49/) |
2121

2222
## Problem Description
@@ -34,7 +34,7 @@ You can return the answer in any order.
3434
```plaintext
3535
Input: nums = [2,7,11,15], target = 9
3636
Output: [0,1]
37-
```
37+
```
3838

3939
**`Example 2:`**
4040

@@ -67,7 +67,9 @@ Output: [0,1]
6767

6868
The problem can be solved using a brute force approach, a hash table, or the two-pointer technique. The brute force approach has a time complexity of $$O(n^2)$$, while the hash table and two-pointer techniques have a time complexity of $$O(n)$$. The hash table approach is the most efficient and is recommended for large inputs.
6969

70-
### Approach 1: Brute Force (Naive)
70+
<Tabs>
71+
<tabItem value="Brute Force" label="Brute Force">
72+
### Approach 1: Brute Force (Naive)
7173

7274
The brute force approach is simple. We iterate through each element `nums[i]` and check if there is another element `nums[j]` such that `nums[i] + nums[j] == target`. If we find such a pair, we return the indices `[i, j]`.
7375

@@ -92,8 +94,12 @@ function twoSumProblem() {
9294
const result = twoSum(nums, target);
9395
return (
9496
<div>
95-
<p><b>Input:</b> nums = {"[", nums.join(", "), "]"}, target = {target}</p>
96-
<p><b>Output:</b> {"[", result.join(", "), "]"}</p>
97+
<p>
98+
<b>Input:</b> nums = {("[", nums.join(", "), "]")}, target = {target}
99+
</p>
100+
<p>
101+
<b>Output:</b> {("[", result.join(", "), "]")}
102+
</p>
97103
</div>
98104
);
99105
}
@@ -116,6 +122,7 @@ function twoSumProblem() {
116122
return [];
117123
}
118124
```
125+
119126
</TabItem>
120127
<TabItem value="TypeScript" label="TypeScript">
121128
```typescript
@@ -131,6 +138,7 @@ function twoSumProblem() {
131138
return [];
132139
}
133140
```
141+
134142
</TabItem>
135143
<TabItem value="Python" label="Python">
136144
```python
@@ -141,8 +149,9 @@ function twoSumProblem() {
141149
if nums[i] + nums[j] == target:
142150
return [i, j]
143151
144-
return []
152+
return []
145153
```
154+
146155
</TabItem>
147156
<TabItem value="Java" label="Java">
148157
```java
@@ -160,6 +169,7 @@ function twoSumProblem() {
160169
}
161170
}
162171
```
172+
163173
</TabItem>
164174
<TabItem value="C++" label="C++">
165175
```cpp
@@ -178,10 +188,10 @@ function twoSumProblem() {
178188
}
179189
};
180190
```
191+
181192
</TabItem>
182193
</Tabs>
183194

184-
185195
#### Complexity Analysis
186196

187197
- Time Complexity: $$O(n^2)$$
@@ -191,6 +201,9 @@ function twoSumProblem() {
191201
- The space complexity is $$O(1)$$ because we are not using any extra space.
192202
- This approach is not efficient and is not recommended for large inputs.
193203

204+
</tabItem>
205+
<tabItem value="Hash Table" label="Hash Table">
206+
194207
### Approach 2: Using Hash Table
195208

196209
We can solve this problem more efficiently using a hash table. We iterate through the array and store the elements and their indices in a hash table. For each element `nums[i]`, we calculate the complement `target - nums[i]` and check if the complement is present in the hash table. If it is present, we return the indices `[i, numMap.get(complement)]`. If the complement is not present, we add the element `nums[i]` to the hash table. If no pair is found, we return an empty array.
@@ -202,25 +215,29 @@ function twoSumProblem() {
202215
const nums = [2, 7, 11, 15];
203216
const target = 9;
204217
205-
const twoSum = function (nums, target) {
206-
const numMap = new Map();
218+
const twoSum = function (nums, target) {
219+
const numMap = new Map();
207220
208-
for (let i = 0; i < nums.length; i++) {
209-
const complement = target - nums[i];
210-
if (numMap.has(complement)) {
211-
return [numMap.get(complement), i];
212-
}
213-
numMap.set(nums[i], i);
214-
}
221+
for (let i = 0; i < nums.length; i++) {
222+
const complement = target - nums[i];
223+
if (numMap.has(complement)) {
224+
return [numMap.get(complement), i];
225+
}
226+
numMap.set(nums[i], i);
227+
}
215228
216-
return [];
217-
};
229+
return [];
230+
};
218231
219232
const result = twoSum(nums, target);
220233
return (
221234
<div>
222-
<p><b>Input:</b> nums = {"[", nums.join(", "), "]"}, target = {target}</p>
223-
<p><b>Output:</b> {"[", result.join(", "), "]"}</p>
235+
<p>
236+
<b>Input:</b> nums = {("[", nums.join(", "), "]")}, target = {target}
237+
</p>
238+
<p>
239+
<b>Output:</b> {("[", result.join(", "), "]")}
240+
</p>
224241
</div>
225242
);
226243
}
@@ -245,6 +262,7 @@ function twoSumProblem() {
245262
return [];
246263
}
247264
```
265+
248266
</TabItem>
249267
<TabItem value="TypeScript" label="TypeScript">
250268
```typescript
@@ -262,6 +280,7 @@ function twoSumProblem() {
262280
return [];
263281
}
264282
```
283+
265284
</TabItem>
266285
<TabItem value="Python" label="Python">
267286
```python
@@ -276,6 +295,7 @@ function twoSumProblem() {
276295
277296
return []
278297
```
298+
279299
</TabItem>
280300
<TabItem value="Java" label="Java">
281301
```java
@@ -295,6 +315,7 @@ function twoSumProblem() {
295315
}
296316
}
297317
```
318+
298319
</TabItem>
299320
<TabItem value="C++" label="C++">
300321
```cpp
@@ -315,10 +336,10 @@ function twoSumProblem() {
315336
}
316337
};
317338
```
339+
318340
</TabItem>
319341
</Tabs>
320342

321-
322343
#### Complexity Analysis
323344

324345
- Time Complexity: $$O(n)$$
@@ -331,6 +352,9 @@ function twoSumProblem() {
331352
- The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table.
332353
- The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$.
333354

355+
</tabItem>
356+
<tabItem value="Two Pointer" label="Two Pointer">
357+
334358
### Approach 3: Using Two Pointers
335359

336360
We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, `left` and `right`, to find the two numbers that add up to the target sum. We initialize `left` to `0` and `right` to `nums.length - 1`. We calculate the sum of the two numbers at the `left` and `right` pointers. If the sum is equal to the target, we return the indices `[left, right]`. If the sum is less than the target, we increment the `left` pointer. If the sum is greater than the target, we decrement the `right` pointer. If no pair is found, we return an empty array.
@@ -366,8 +390,12 @@ function twoSumProblem() {
366390
const result = twoSum(nums, target);
367391
return (
368392
<div>
369-
<p><b>Input:</b> nums = {"[", nums.join(", "), "]"}, target = {target}</p>
370-
<p><b>Output:</b> {"[", result.join(", "), "]"}</p>
393+
<p>
394+
<b>Input:</b> nums = {("[", nums.join(", "), "]")}, target = {target}
395+
</p>
396+
<p>
397+
<b>Output:</b> {("[", result.join(", "), "]")}
398+
</p>
371399
</div>
372400
);
373401
}
@@ -399,6 +427,7 @@ function twoSumProblem() {
399427
return [];
400428
}
401429
```
430+
402431
</TabItem>
403432
<TabItem value="TypeScript" label="TypeScript">
404433
```typescript
@@ -423,6 +452,7 @@ function twoSumProblem() {
423452
return [];
424453
}
425454
```
455+
426456
</TabItem>
427457
<TabItem value="Python" label="Python">
428458
```python
@@ -443,6 +473,7 @@ function twoSumProblem() {
443473
444474
return []
445475
```
476+
446477
</TabItem>
447478
<TabItem value="Java" label="Java">
448479
```java
@@ -473,6 +504,7 @@ function twoSumProblem() {
473504
}
474505
}
475506
```
507+
476508
</TabItem>
477509
<TabItem value="C++" label="C++">
478510
```cpp
@@ -506,6 +538,7 @@ function twoSumProblem() {
506538
}
507539
};
508540
```
541+
509542
</TabItem>
510543
</Tabs>
511544

@@ -519,6 +552,9 @@ function twoSumProblem() {
519552
- This approach is efficient and is recommended for large inputs.
520553
- The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$.
521554

555+
</tabItem>
556+
</Tabs>
557+
522558
:::tip Note
523559
**Which is the best approach? and why?**
524560

@@ -528,4 +564,4 @@ The hash table approach is the most efficient and is recommended for large input
528564

529565
## Conclusion
530566

531-
In this tutorial, we learned how to solve the Two Sum problem on LeetCode using different approaches. We discussed the brute force approach, the hash table approach, and the two-pointer approach. We implemented the solutions in JavaScript, TypeScript, Python, Java, and C++. We also analyzed the time and space complexity of each approach and compared them to determine the best approach for this problem. The hash table approach is the most efficient and is recommended for large inputs.
567+
In this tutorial, we learned how to solve the Two Sum problem on LeetCode using different approaches. We discussed the brute force approach, the hash table approach, and the two-pointer approach. We implemented the solutions in JavaScript, TypeScript, Python, Java, and C++. We also analyzed the time and space complexity of each approach and compared them to determine the best approach for this problem. The hash table approach is the most efficient and is recommended for large inputs.

0 commit comments

Comments
 (0)