From 3e5c6c9ecd577bc20346c42421d83cc59c8774ea Mon Sep 17 00:00:00 2001 From: Nihhaar Saini Date: Sun, 23 Nov 2025 11:05:42 +0530 Subject: [PATCH] Enhance Two Sum Python solution with explanations and formatting --- Python/1_TwoSum.py | 112 ++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/Python/1_TwoSum.py b/Python/1_TwoSum.py index cb9359c9..1b16da26 100644 --- a/Python/1_TwoSum.py +++ b/Python/1_TwoSum.py @@ -1,56 +1,94 @@ -#Difficulty = Easy -#Submission Speed = 64.69% +from typing import List + +# Difficulty = Easy +# Submission Speed ≈ 64.69% + ''' -Solution-1: -Example: nums = [3,2,1,4] target = 6 -Step1) Create a dictionary and populate it with elements of nums as the key and their corresponding index as values. - +Solution-1: Two-pass Hash Table + +Example: + nums = [3, 2, 1, 4] + target = 6 + +Step 1) +Create a dictionary and populate it with elements of nums as the key +and their corresponding index as values. + d = { - 3:0, - 2:1 - 1:2 - 4:3 - } -Step2) Traverse the array and check for every element, if complement (target-element) exists in dictionary. Also check, -the index of (target-element) is not same as that of element (using dictionary as we have saved index as values.) + 3: 0, + 2: 1, + 1: 2, + 4: 3 + } + +Step 2) +Traverse the array and for every element, check if the complement +(target - element) exists in the dictionary. + +Also check that the index of (target - element) is not the same as +the current index (we use the dictionary values to get indices). + Traversing the array: -# i=0 3,2,1,4 - ^ - checking if (6-3) exists in dictionary? - Yes it exists - but d[6-3]==i (We are adding the same element to get the target which is invalid according to the question) + i = 0 nums = [3, 2, 1, 4] + ^ + Check if (6 - 3) exists in dictionary: + Yes, it exists (3 is in d), + but d[6 - 3] == i + This means we are using the same element twice, which is invalid. -Step3) If we found a valid pair, then we return [i,d[complement]] -''' -''' -Time Complexity = O(N) -Space Complexity = O(N) +Step 3) +If we find a valid pair, we return [i, d[complement]]. + +Time Complexity: O(N) +Space Complexity: O(N) ''' -#Two-pass +# Two-pass class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - d = {v:i for i,v in enumerate(nums)} + # First pass: build value -> index mapping + d = {v: i for i, v in enumerate(nums)} + + # Second pass: check for each element if its complement exists for i in range(len(nums)): complement = target - nums[i] - if complement in d and d[complement]!=i: - return [i,d[complement]] + # Ensure complement exists and is not the same index + if complement in d and d[complement] != i: + return [i, d[complement]] + ''' -Solution-2: Single Pass: -Instead of doing double passes (one pass while populating the dictionary and other while checking for complement), -We do this in Single Pass. that is, checking for complement while populating the dictionary. -Before inserting the element in dictionary, we check whether the complement already exists, if exists, we return both indexes -(index of current element and index of its complement) and if not exists, we insert the element in dictionary. -This way we wont have to check if the index of element and its complement is same or not. +Solution-2: Single Pass Hash Table (Optimized) + +Instead of doing two passes (one to populate the dictionary and +another to check for complements), we do this in a single pass. + +Idea: + While traversing the array: + 1) For the current element nums[i], compute complement = target - nums[i] + 2) Check if complement is already present in the dictionary: + - If yes → we have found our answer: [index of complement, i] + 3) If not present → store nums[i] with its index in the dictionary. + +This way: + - We don't need to worry about using the same index twice. + - We only traverse the list once. + +Time Complexity: O(N) +Space Complexity: O(N) ''' -#Single Pass +# Single-pass (this class overrides the previous one in LeetCode-style usage) class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - d = {} + d = {} # value -> index + for i in range(len(nums)): complement = target - nums[i] + + # If we have already seen the complement, return the pair of indices if complement in d: - return [d[complement],i] - d[nums[i]]=i + return [d[complement], i] + + # Otherwise, store the current value and its index + d[nums[i]] = i