Skip to content

resolved and write in better way for dsa solution #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 61 additions & 25 deletions dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ description: "This is a solution to the Two Sum problem on LeetCode."

## Problem Description

|Problem Statement | Solution Link | LeetCode Profile|
|:---|:---|:---|
| Problem Statement | Solution Link | LeetCode Profile |
| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- |
| [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/) |

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

**`Example 2:`**

Expand Down Expand Up @@ -67,7 +67,9 @@ Output: [0,1]

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.

### Approach 1: Brute Force (Naive)
<Tabs>
<tabItem value="Brute Force" label="Brute Force">
### Approach 1: Brute Force (Naive)

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]`.

Expand All @@ -92,8 +94,12 @@ function twoSumProblem() {
const result = twoSum(nums, target);
return (
<div>
<p><b>Input:</b> nums = {"[", nums.join(", "), "]"}, target = {target}</p>
<p><b>Output:</b> {"[", result.join(", "), "]"}</p>
<p>
<b>Input:</b> nums = {("[", nums.join(", "), "]")}, target = {target}
</p>
<p>
<b>Output:</b> {("[", result.join(", "), "]")}
</p>
</div>
);
}
Expand All @@ -116,6 +122,7 @@ function twoSumProblem() {
return [];
}
```

</TabItem>
<TabItem value="TypeScript" label="TypeScript">
```typescript
Expand All @@ -131,6 +138,7 @@ function twoSumProblem() {
return [];
}
```

</TabItem>
<TabItem value="Python" label="Python">
```python
Expand All @@ -141,8 +149,9 @@ function twoSumProblem() {
if nums[i] + nums[j] == target:
return [i, j]

return []
return []
```

</TabItem>
<TabItem value="Java" label="Java">
```java
Expand All @@ -160,6 +169,7 @@ function twoSumProblem() {
}
}
```

</TabItem>
<TabItem value="C++" label="C++">
```cpp
Expand All @@ -178,10 +188,10 @@ function twoSumProblem() {
}
};
```

</TabItem>
</Tabs>


#### Complexity Analysis

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

</tabItem>
<tabItem value="Hash Table" label="Hash Table">

### Approach 2: Using Hash Table

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.
Expand All @@ -202,25 +215,29 @@ function twoSumProblem() {
const nums = [2, 7, 11, 15];
const target = 9;

const twoSum = function (nums, target) {
const numMap = new Map();
const twoSum = function (nums, target) {
const numMap = new Map();

for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.has(complement)) {
return [numMap.get(complement), i];
}
numMap.set(nums[i], i);
}
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.has(complement)) {
return [numMap.get(complement), i];
}
numMap.set(nums[i], i);
}

return [];
};
return [];
};

const result = twoSum(nums, target);
return (
<div>
<p><b>Input:</b> nums = {"[", nums.join(", "), "]"}, target = {target}</p>
<p><b>Output:</b> {"[", result.join(", "), "]"}</p>
<p>
<b>Input:</b> nums = {("[", nums.join(", "), "]")}, target = {target}
</p>
<p>
<b>Output:</b> {("[", result.join(", "), "]")}
</p>
</div>
);
}
Expand All @@ -245,6 +262,7 @@ function twoSumProblem() {
return [];
}
```

</TabItem>
<TabItem value="TypeScript" label="TypeScript">
```typescript
Expand All @@ -262,6 +280,7 @@ function twoSumProblem() {
return [];
}
```

</TabItem>
<TabItem value="Python" label="Python">
```python
Expand All @@ -276,6 +295,7 @@ function twoSumProblem() {

return []
```

</TabItem>
<TabItem value="Java" label="Java">
```java
Expand All @@ -295,6 +315,7 @@ function twoSumProblem() {
}
}
```

</TabItem>
<TabItem value="C++" label="C++">
```cpp
Expand All @@ -315,10 +336,10 @@ function twoSumProblem() {
}
};
```

</TabItem>
</Tabs>


#### Complexity Analysis

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

</tabItem>
<tabItem value="Two Pointer" label="Two Pointer">

### Approach 3: Using Two Pointers

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.
Expand Down Expand Up @@ -366,8 +390,12 @@ function twoSumProblem() {
const result = twoSum(nums, target);
return (
<div>
<p><b>Input:</b> nums = {"[", nums.join(", "), "]"}, target = {target}</p>
<p><b>Output:</b> {"[", result.join(", "), "]"}</p>
<p>
<b>Input:</b> nums = {("[", nums.join(", "), "]")}, target = {target}
</p>
<p>
<b>Output:</b> {("[", result.join(", "), "]")}
</p>
</div>
);
}
Expand Down Expand Up @@ -399,6 +427,7 @@ function twoSumProblem() {
return [];
}
```

</TabItem>
<TabItem value="TypeScript" label="TypeScript">
```typescript
Expand All @@ -423,6 +452,7 @@ function twoSumProblem() {
return [];
}
```

</TabItem>
<TabItem value="Python" label="Python">
```python
Expand All @@ -443,6 +473,7 @@ function twoSumProblem() {

return []
```

</TabItem>
<TabItem value="Java" label="Java">
```java
Expand Down Expand Up @@ -473,6 +504,7 @@ function twoSumProblem() {
}
}
```

</TabItem>
<TabItem value="C++" label="C++">
```cpp
Expand Down Expand Up @@ -506,6 +538,7 @@ function twoSumProblem() {
}
};
```

</TabItem>
</Tabs>

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

</tabItem>
</Tabs>

:::tip Note
**Which is the best approach? and why?**

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

## Conclusion

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.
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.
Loading