Skip to content

Commit

Permalink
Update PrepVerse
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashSingh3031 committed Nov 7, 2024
1 parent 3692dfd commit 49543ee
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 38 deletions.
118 changes: 118 additions & 0 deletions documentation/blog/2024-08-07-two-sum-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: Two Sum Explained
description: DSA problem two sum explained
slug: two-sum
authors: AkashSingh3031
tags: [Array, LeetCode]
is_featured: false
image: https://miro.medium.com/v2/resize:fit:1400/1*2x-CAwfeui5YM4148VxgRA.jpeg
hide_table_of_contents: false
---

## PrepVerse | Three Approaches to Solving the Two Sum Problem

### Approach 1: Brute Force

**Algorithm:**

The brute force approach is straightforward. Loop through each element `x` in the array and check if there is another element that equals `target - x`.

**Implementation:**

<Tabs smallTabs>
<TabItem value="py" label="Python">

```python title="Brute Force"
def two_sum_brute_force(nums, target):
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return None
```

</TabItem>
</Tabs>

**Complexity Analysis:**

- **Time Complexity:** O(n²)
- For each element, we loop through the rest of the array to find its complement, resulting in O(n) operations per element. Hence, the overall time complexity is O(n²).
- **Space Complexity:** O(1)
- The space required does not depend on the size of the input array, so only constant space is used.

### Approach 2: Two-pass Hash Table

**Intuition:**

To improve our runtime complexity, we need a more efficient way to check if the complement exists in the array. A hash table is well-suited for this purpose because it supports fast lookups in near constant time. By trading space for speed, we can reduce the lookup time from O(n) to O(1).

**Algorithm:**

1. In the first iteration, add each element's value as a key and its index as a value to the hash table.
2. In the second iteration, check if each element's complement (target - nums[i]) exists in the hash table. If it does, return the current element's index and its complement's index.

**Implementation:**

<Tabs smallTabs>
<TabItem value="py" label="Python">

```python title="Two-pass Hash Table"
def two_sum_two_pass_hash_table(nums, target):
hash_table = {}
for i, num in enumerate(nums):
hash_table[num] = i

for i, num in enumerate(nums):
complement = target - num
if complement in hash_table and hash_table[complement] != i:
return [i, hash_table[complement]]
return None
```

</TabItem>
</Tabs>

**Complexity Analysis:**

- **Time Complexity:** O(n)
- We traverse the list containing `n` elements exactly twice. Since the hash table reduces the lookup time to O(1), the overall time complexity is O(n).
- **Space Complexity:** O(n)
- The extra space required depends on the number of items stored in the hash table, which stores exactly `n` elements.

### Approach 3: One-pass Hash Table

**Algorithm:**

We can optimize further by performing the lookup and insertion in a single pass. While iterating through the array, we check if the current element's complement already exists in the hash table. If it does, we have found a solution and return the indices immediately.

**Implementation:**

<Tabs smallTabs>
<TabItem value="py" label="Python">

```python title="One-pass Hash Table"
def two_sum_one_pass_hash_table(nums, target):
hash_table = {}
for i, num in enumerate(nums):
complement = target - num
if complement in hash_table:
return [hash_table[complement], i]
hash_table[num] = i
return None
```

</TabItem>
</Tabs>

**Complexity Analysis:**

- **Time Complexity:** O(n)
- We traverse the list containing `n` elements only once. Each lookup in the table costs only O(1) time.
- **Space Complexity:** O(n)
- The extra space required depends on the number of items stored in the hash table, which stores at most `n` elements.

### Conclusion

In summary, the brute force approach is simple but inefficient, with a time complexity of O(n²). The two-pass hash table approach improves the time complexity to O(n) by utilizing extra space for a hash table. The one-pass hash table approach further optimizes the solution by combining lookup and insertion into a single pass, maintaining an overall time complexity of O(n) and space complexity of O(n). Depending on the constraints and requirements of your problem, choosing the right approach can significantly improve performance.
2 changes: 1 addition & 1 deletion documentation/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
},
{
type: "link",
href: "/prepverse-community",
href: "https://prepverse.vercel.app/prepverse-community",
label: "Prepverse Community",
className: "enterprise-badge",
},
Expand Down
4 changes: 2 additions & 2 deletions documentation/src/assets/nav-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const POPOVERMENUS: NavMenu[] = [
{
label: "Documents",
description: "Everything you need to get started",
link: "/docs/",
link: "https://prepverse.vercel.app/docs/",
icon: DocumentsIcon,
},
{
Expand Down Expand Up @@ -103,7 +103,7 @@ export const POPOVERMENUS: NavMenu[] = [
{
label: "About Us",
description: "My information",
link: "/AkashSingh3031",
link: "https://prepverse.vercel.app/AkashSingh3031/",
icon: AboutUsIcon,
},
// {
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Home() {
<meta property="og:title" content={title} />
<link
rel="preload"
href="https://prepverse.github.io"
href="https://prepverse.vercel.app"
as="document"
/>
</Head>
Expand Down
63 changes: 60 additions & 3 deletions documentation/src/prepverse-theme/about-section/edu-exp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,64 @@ const Experience = ({ className }: { className?: string }) => {
"font-semibold",
)}
>
Feb 2023 — Present (1 year)
Feb 2023 — Present
</h2>
<div
className={clsx(
"flex flex-ṛow gap-5",
)}
>
<div className="relative">
<img
className={clsx(
"h-[50px]",
"rounded-full",
)}
src="https://www.enatyam.com/public/images/logos.webp"
alt="Enatyam India"
/>
</div>
<div className="text-xs md:text-base">
<h2
className={clsx(
"text-base landing-sm:text-xl",
"dark:text-gray-300 text-gray-900",
"font-semibold",
)}
>
Enatyam India
</h2>
<p
className={clsx(
"text-base",
"dark:text-gray-400 text-gray-600",
)}
>
Web Developer | SEO Specialist
</p>
</div>
</div>
</div>
<div
className={clsx(
"flex flex-col gap-5",
"dark:bg-enterprise-data-source-dark dark:bg-gray-800 bg-gray-0",
"bg-blend-overlay",
"bg-no-repeat",
"rounded-2xl landing-sm:rounded-3xl",
"dark:bg-gray-900",
"rounded-xl p-4",
"border border-b-4 dark:border-gray-700 border-gray-200",
)}
>
<h2
className={clsx(
"text-base",
"dark:text-gray-400 text-gray-600",
"font-semibold",
)}
>
Feb 2023 — July 2024 (1.5 years)
</h2>
<div
className={clsx(
Expand Down Expand Up @@ -248,7 +305,7 @@ const Experience = ({ className }: { className?: string }) => {
</div>
</div>
</div>
<div
{/* <div
className={clsx(
"flex flex-col gap-5",
"dark:bg-enterprise-data-source-dark dark:bg-gray-800 bg-gray-0",
Expand Down Expand Up @@ -304,7 +361,7 @@ const Experience = ({ className }: { className?: string }) => {
</p>
</div>
</div>
</div>
</div> */}
<div
className={clsx(
"flex flex-col gap-5",
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/prepverse-theme/common-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const CommonHeader = ({
)}
>
<Link
to="/"
to="https://prepverse.vercel.app"
// onContextMenu={openFigma}
>
<PrepVerseLogoIcon className="text-gray-900 dark:text-gray-0" />
Expand Down
12 changes: 6 additions & 6 deletions documentation/src/prepverse-theme/common-header/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ export const MENU_ITEMS: MenuItemType[] = [
{
label: "Foundation Docs",
description: "Learn C++, Python & Java",
link: "/docs",
link: "https://prepverse.vercel.app/docs",
icon: UseCasesIcon,
},
{
label: "DSA Docs",
description: "Learning DSA in C++, Python & Java",
link: "/docs/1.DS.Algo",
link: "https://prepverse.vercel.app/docs/1.DS.Algo",
icon: DocumentsIcon,
},
// {
Expand Down Expand Up @@ -107,13 +107,13 @@ export const MENU_ITEMS: MenuItemType[] = [
{
label: "About Me",
description: "My information.",
link: "/AkashSingh3031",
link: "https://prepverse.vercel.app/AkashSingh3031",
icon: ContributeIcon,
},
{
label: "Project Showcase",
description: "PrepVerse open-source projects",
link: "/showcase",
link: "https://prepverse.vercel.app/showcase",
icon: TutorialIcon,
},
{
Expand All @@ -127,13 +127,13 @@ export const MENU_ITEMS: MenuItemType[] = [
{
isPopover: false,
label: "Blog",
href: "/blog",
href: "https://prepverse.vercel.app/blog",
icon: BlogIcon,
},
{
isPopover: false,
label: "PrepVerse Community",
href: "/prepverse-community",
href: "https://prepverse.vercel.app/prepverse-community",
icon: CommunityIcon,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const PrepVerseLogo = ({ title, className }: Props) => {
return (
<div className={clsx("flex", "items-center", className)}>
<Link
to="/docs"
to="https://prepverse.vercel.app/docs"
className={clsx(
"flex",
"items-center justify-center",
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/prepverse-theme/doc-breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const DocBreadcrumbs = () => {
itemProp="itemListElement"
itemType="https://schema.org/ListItem"
>
<Link href="/docs" itemProp="item">
<Link href="https://prepverse.vercel.app/docs" itemProp="item">
<HomeIcon className="text-gray-400 dark:text-gray-500" />
<span className="sr-only" itemProp="name">
Documentation
Expand Down
4 changes: 2 additions & 2 deletions documentation/src/prepverse-theme/doc-prepverse-logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const DocPrepVerseLogo = ({ className }: Props) => {
)}
>
<Link
to="/"
to="https://prepverse.vercel.app"
className={clsx("no-underline", "flex items-center gap-2")}
// onContextMenu={openFigma}
>
Expand All @@ -42,7 +42,7 @@ export const DocPrepVerseLogo = ({ className }: Props) => {
"bg-gray-300 dark:bg-gray-600",
)}
/>
<Link to="/docs" className={clsx("no-underline")}>
<Link to="https://prepverse.vercel.app/docs" className={clsx("no-underline")}>
<span
className={clsx(
"text-gray-1000 dark:text-gray-0",
Expand Down
10 changes: 5 additions & 5 deletions documentation/src/prepverse-theme/footer-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ export const menuItems = [
items: [
{
label: "Getting Started",
href: "/docs",
href: "https://prepverse.vercel.app/docs",
},
{
label: "Blog",
href: "/blog",
href: "https://prepverse.vercel.app/blog",
},
{
label: "Project Showcase",
icon: <NewBadgeIcon />,
href: "/showcase",
href: "https://prepverse.vercel.app/showcase",
},
{
label: "Community",
icon: <NewBadgeIcon />,
href: "/prepverse-community",
href: "https://prepverse.vercel.app/prepverse-community",
},
],
},
Expand All @@ -54,7 +54,7 @@ export const menuItems = [
items: [
{
label: "About Me",
href: "/AkashSingh3031",
href: "https://prepverse.vercel.app/AkashSingh3031",
},
{
label: "Portfolio",
Expand Down
Loading

0 comments on commit 49543ee

Please sign in to comment.