DDileep OS
AboutProjectsJourneyBlogBooksContact
Available
--:--
DDileep OS

Building products, learning in public and becoming a better software engineer every day.

Available for opportunitiesv1.0.0

Explore

HomeAboutProjectsJourneyBlogContact

Resources

BooksLearningDSAJavaUsesResume

More

ExperienceAchievementsNowGuestbook

Connect

Follow my work, connect professionally, or drop me an email anytime.

© 2026 Dileep OS · Built with Next.js, Sanity and Tailwind CSS.

Back to problemsSolve on LeetCode
EasyLeetCodeSolvedFeatured

Two Sum

HashMap can reduce nested loop problems from O(n²) to O(n).

ArrayHashMap

Problem stats

Solved on7/15/2026
Attempts2
Time complexityO(n)
Space complexityO(n)
Solution strategy

From brute force to optimal

Before

Brute force

Use two nested loops and check every pair of numbers. Return the indices when their sum equals the target.

VS

After

Optimal

Store previously visited numbers inside a HashMap. For every number check if target-currentNumber already exists.

Java Implementation

Complete Java Solution

The optimized implementation, ready for interview preparation and quick revision.

35Lines
22Code Lines
764Characters

Solution.java

Java
1import java.util.HashMap;
2import java.util.Map;
3
4public class Solution {
5
6 public int[] twoSum(int[] nums, int target) {
7
8 Map<Integer, Integer> map = new HashMap<>();
9
10 for (int i = 0; i < nums.length; i++) {
11
12 int complement = target - nums[i];
13
14 if (map.containsKey(complement)) {
15 return new int[] { map.get(complement), i };
16 }
17
18 map.put(nums[i], i);
19 }
20
21 return new int[] {};
22 }
23
24 public static void main(String[] args) {
25
26 Solution solution = new Solution();
27
28 int[] nums = {2, 7, 11, 15};
29 int target = 9;
30
31 int[] result = solution.twoSum(nums, target);
32
33 System.out.println("[" + result[0] + ", " + result[1] + "]");
34 }
35}
35 lines764 chars
Complexity analysis

Performance breakdown

Analyze the efficiency of the algorithm by understanding its time and space complexity across different execution scenarios.

Time complexity

O(n)

Space complexity

O(n)

By execution case

Best case

O(n)

Average case

O(n)

Worst case

O(n)

Complexity summary

This solution achieves a O(n) time complexity while using O(n) extra memory. It is considered the optimal approach for this problem and is suitable for coding interviews as well as competitive programming.

Key Learning

What You Should Remember

Every coding problem teaches a pattern. Focus on the concepts, avoid common mistakes, and remember the interview-worthy takeaways instead of memorizing code.

Core Learning

The biggest takeaway from this problem.

HashMap can reduce nested loop problems from O(n²) to O(n).

Interview Tip

Explain why the optimized solution works before writing the final code. Interviewers care about your thinking process as much as your implementation.

Common Mistake

Avoid jumping directly to coding. Always analyze edge cases, constraints, and the optimal approach before implementation.

Revision Note

Focus on understanding the algorithm's pattern instead of memorizing the code. Once the logic becomes clear, implementing the solution in any programming language becomes much easier.

Final Takeaway

Every DSA problem introduces a reusable pattern. Instead of remembering the exact solution, remember the thought process that led to it. Over time, these patterns will help you solve new problems much faster and perform better in coding interviews.

Learn the pattern, not the code.

Continue learning

Practice consistently and move through the roadmap one problem at a time.

Previous

Invert Binary Tree

Next

Valid Anagram

All problems
Keep practicing

Related problems

Practice similar problems to strengthen your understanding of the underlying algorithm and improve pattern recognition.

Easy

Best Time to Buy and Sell Stock

ArrayGreedy
Solve now
Easy

Contains Duplicate

ArrayHashSet
Solve now
Medium

Search in Rotated Sorted Array

Binary SearchArray
Solve now