Skip to main content

Command Palette

Search for a command to run...

LeetCode 1: Two Sum

Solution for LeetCode Two Sum Problem

Updated
2 min read
LeetCode 1: Two Sum
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: January 15, 2026
Category: Arrays | HashMap
Time Taken: 15 minutes
Difficulty: Easy


Problem Statement

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Link: Two Sum


My Approach

Initial thought:

  • The most brute-force solution is to use 2 for loops to compare each pair of values and check if they add up to the target. Its time complexity would be O(N²) and space complexity would be O(1).

  • The second solution I considered was to sort the array and use the two-pointer method to compare the sum of the maximum and minimum values to the target. However, this won't work because we need to return their original indices.

  • The third solution is to map the values to their indices and use a for loop to check if the difference between the target and the current value exists in the hashmap. This solution will have a space and time complexity of O(N) for both.

Final solution:

  • First, create a HashMap to store the values and their indices.

  • Use a for loop with enumerate to get the indices and values of the array.

  • Check if diff = target - currValue is in the HashMap. If it is, return the current index and the index of the value in the HashMap.

  • If it's not there, check if the target is less than the sum; if so, decrement the right pointer. If the target is greater than the sum, increment the left pointer.

Why this works:
This works because we only need to compare the target with the difference and store all the values in the hashmap that currently don't match our difference.


Solution Code

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashmap = {}

        for currIndex, currValue in enumerate(nums):
            diff = target - currValue
            if diff in hashmap:
                return [currIndex, hashmap[diff]]
            else:
                hashmap[currValue] = currIndex

Complexity:

Time: O(N) Space: O(N)

Key Takeaway: Using enumerate makes it easy to get both the index and value from an array.

Pattern: Hashing.

Mistakes I Made: I did not read the complete problem and rushed to solve it, which resulted in time being lost.

Series: 90 Days of Data Engineering Progress: 6/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 7 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 182: Duplicate Emails

Easy Approach to Solve LeetCode 182