diff --git a/documentation/blog/2024-08-07-two-sum-explain.md b/documentation/blog/2024-08-07-two-sum-explain.md new file mode 100644 index 0000000..8894732 --- /dev/null +++ b/documentation/blog/2024-08-07-two-sum-explain.md @@ -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:** + + + + +```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 +``` + + + + +**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:** + + + + +```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 +``` + + + + +**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:** + + + + +```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 +``` + + + + +**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. \ No newline at end of file diff --git a/documentation/sidebars.js b/documentation/sidebars.js index 0aecaed..a36357a 100644 --- a/documentation/sidebars.js +++ b/documentation/sidebars.js @@ -27,7 +27,7 @@ module.exports = { }, { type: "link", - href: "/prepverse-community", + href: "https://prepverse.vercel.app/prepverse-community", label: "Prepverse Community", className: "enterprise-badge", }, diff --git a/documentation/src/assets/nav-menu.ts b/documentation/src/assets/nav-menu.ts index bbc3d7a..a087a2f 100644 --- a/documentation/src/assets/nav-menu.ts +++ b/documentation/src/assets/nav-menu.ts @@ -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, }, { @@ -103,7 +103,7 @@ export const POPOVERMENUS: NavMenu[] = [ { label: "About Us", description: "My information", - link: "/AkashSingh3031", + link: "https://prepverse.vercel.app/AkashSingh3031/", icon: AboutUsIcon, }, // { diff --git a/documentation/src/pages/index.tsx b/documentation/src/pages/index.tsx index e4b5581..409889b 100644 --- a/documentation/src/pages/index.tsx +++ b/documentation/src/pages/index.tsx @@ -21,7 +21,7 @@ function Home() { diff --git a/documentation/src/prepverse-theme/about-section/edu-exp.tsx b/documentation/src/prepverse-theme/about-section/edu-exp.tsx index 96c02d3..ccdd4f5 100644 --- a/documentation/src/prepverse-theme/about-section/edu-exp.tsx +++ b/documentation/src/prepverse-theme/about-section/edu-exp.tsx @@ -210,7 +210,64 @@ const Experience = ({ className }: { className?: string }) => { "font-semibold", )} > - Feb 2023 — Present (1 year) + Feb 2023 — Present + +
+
+ Enatyam India +
+
+

+ Enatyam India +

+

+ Web Developer | SEO Specialist +

+
+
+ +
+

+ Feb 2023 — July 2024 (1.5 years)

{
-
{

- + */}
diff --git a/documentation/src/prepverse-theme/common-header/constants.ts b/documentation/src/prepverse-theme/common-header/constants.ts index f35982c..06cfc82 100644 --- a/documentation/src/prepverse-theme/common-header/constants.ts +++ b/documentation/src/prepverse-theme/common-header/constants.ts @@ -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, }, // { @@ -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, }, { @@ -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, }, ]; diff --git a/documentation/src/prepverse-theme/common-prepverse-logo.tsx b/documentation/src/prepverse-theme/common-prepverse-logo.tsx index 8c9e5e6..2efd83d 100644 --- a/documentation/src/prepverse-theme/common-prepverse-logo.tsx +++ b/documentation/src/prepverse-theme/common-prepverse-logo.tsx @@ -13,7 +13,7 @@ export const PrepVerseLogo = ({ title, className }: Props) => { return (
{ itemProp="itemListElement" itemType="https://schema.org/ListItem" > - + Documentation diff --git a/documentation/src/prepverse-theme/doc-prepverse-logo.tsx b/documentation/src/prepverse-theme/doc-prepverse-logo.tsx index 229fcec..5629f58 100644 --- a/documentation/src/prepverse-theme/doc-prepverse-logo.tsx +++ b/documentation/src/prepverse-theme/doc-prepverse-logo.tsx @@ -19,7 +19,7 @@ export const DocPrepVerseLogo = ({ className }: Props) => { )} > @@ -42,7 +42,7 @@ export const DocPrepVerseLogo = ({ className }: Props) => { "bg-gray-300 dark:bg-gray-600", )} /> - + , - href: "/showcase", + href: "https://prepverse.vercel.app/showcase", }, { label: "Community", icon: , - href: "/prepverse-community", + href: "https://prepverse.vercel.app/prepverse-community", }, ], }, @@ -54,7 +54,7 @@ export const menuItems = [ items: [ { label: "About Me", - href: "/AkashSingh3031", + href: "https://prepverse.vercel.app/AkashSingh3031", }, { label: "Portfolio", diff --git a/documentation/src/prepverse-theme/landing-explore-prepverse.tsx b/documentation/src/prepverse-theme/landing-explore-prepverse.tsx index 1c4f58c..f886fef 100644 --- a/documentation/src/prepverse-theme/landing-explore-prepverse.tsx +++ b/documentation/src/prepverse-theme/landing-explore-prepverse.tsx @@ -17,7 +17,7 @@ const list = [ description: "Master Data Structures and Algorithms with our comprehensive guides, tutorials, and practice problems.", view: , - source: "https://prepverse.github.io/docs/1.DS.Algo", + source: "https://prepverse.vercel.app/docs/1.DS.Algo", }, { icon: , @@ -25,7 +25,7 @@ const list = [ description: "Master web development with our tutorials and resources. Learn HTML, CSS, JavaScript, and more to build stunning websites.", view: , - source: "https://prepverse.github.io/showcase", + source: "https://prepverse.vercel.app/showcase", }, { icon: , @@ -33,7 +33,7 @@ const list = [ description: "Discover cutting-edge resources in AI, ML, and Data Science. Enhance your skills with tutorials, guides, and practice tests.", view: , - source: "https://prepverse.github.io/", + source: "https://prepverse.vercel.app", }, { icon: , @@ -41,7 +41,7 @@ const list = [ description: "Discover a diverse array of programming languages tailored for every project and proficiency level.", view: , - source: "https://prepverse.github.io/docs", + source: "https://prepverse.vercel.app/docs", }, { icon: , @@ -49,7 +49,7 @@ const list = [ description: "Explore essential Computer Science subjects: OS, DBMS, CN, OOPs - learn key concepts and applications in just one click", view: , - source: "https://prepverse.github.io/", + source: "https://prepverse.vercel.app", }, { icon: , @@ -57,7 +57,7 @@ const list = [ description: "Discover the backbone of data management with Databases—efficiently store, retrieve, and manage structured information for applications.", view: , - source: "https://prepverse.github.io/", + source: "https://prepverse.vercel.app", }, ]; @@ -185,7 +185,7 @@ export const LandingExplorePrepVerse: FC = ({ className }) => { ); })}
- + Explore More
diff --git a/documentation/src/prepverse-theme/landing-footer.tsx b/documentation/src/prepverse-theme/landing-footer.tsx index 03feb35..a1a8cca 100644 --- a/documentation/src/prepverse-theme/landing-footer.tsx +++ b/documentation/src/prepverse-theme/landing-footer.tsx @@ -165,7 +165,7 @@ export const LandingFooter = ({ variant = "landing" }: Props) => { )} > { return ( { )} > { )} > - + {/* */} diff --git a/documentation/src/prepverse-theme/top-announcement.tsx b/documentation/src/prepverse-theme/top-announcement.tsx index e083dc2..46d387f 100644 --- a/documentation/src/prepverse-theme/top-announcement.tsx +++ b/documentation/src/prepverse-theme/top-announcement.tsx @@ -111,7 +111,7 @@ export const TopAnnouncement = () => { const Text = () => { return (