🔧 AI Nachrichten The Next Terrorist Attack Is Predictable(10.09.2026 um 23:41 Uhr)
🔧 AI Nachrichten Could A.I. Really Kill All Humans?(10.09.2026 um 23:53 Uhr)
🔧 AI Nachrichten Amazon Prime Video Uses A.I. for Lip-Synced Translations(11.09.2026 um 01:56 Uhr)
🔧 AI Nachrichten McClatchy Makes Deep Job Cuts to Newspapers Around the Country(11.09.2026 um 04:25 Uhr)
🔧 AI Nachrichten Law schools tell students to put AI away(07.09.2026 um 16:37 Uhr)
🔧 AI Nachrichten Can Huawei build China’s answer to ASML?(08.09.2026 um 04:57 Uhr)
🔧 AI Nachrichten AI is ushering in an era of mass toe-treading at work(08.09.2026 um 06:00 Uhr)
🔧 AI Nachrichten The Next Terrorist Attack Is Predictable(10.09.2026 um 23:41 Uhr)
🔧 AI Nachrichten Could A.I. Really Kill All Humans?(10.09.2026 um 23:53 Uhr)
🔧 AI Nachrichten Amazon Prime Video Uses A.I. for Lip-Synced Translations(11.09.2026 um 01:56 Uhr)
🔧 AI Nachrichten McClatchy Makes Deep Job Cuts to Newspapers Around the Country(11.09.2026 um 04:25 Uhr)
🔧 AI Nachrichten Law schools tell students to put AI away(07.09.2026 um 16:37 Uhr)
🔧 AI Nachrichten Can Huawei build China’s answer to ASML?(08.09.2026 um 04:57 Uhr)
🔧 AI Nachrichten AI is ushering in an era of mass toe-treading at work(08.09.2026 um 06:00 Uhr)

🔧 Programmierung 🕛 vor 3 Monaten 2 Min Lesezeit
0

Two Sum | HashMap Pattern

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Problem Link: https://leetcode.com/problems/two-sum/



The Two Sum problem is one of the most frequently asked coding interview questions. While the brute force solution is straightforward, the real interview discussion revolves around identifying the complement pattern and optimizing it using a HashMap.









Problem Statement



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






Example






CODE
Input: nums = [2,7,11,15], target = 9

Output: [0,1]






Because:




CODE
nums[0] + nums[1] = 2 + 7 = 9












Brute Force Approach






Intuition



The most straightforward way is to check every possible pair.



For each element, iterate through all elements after it and verify whether their sum equals the target.



This works but performs many unnecessary comparisons.






Time & Space Complexity




  • Time Complexity: O(N²)

  • Space Complexity: O(1)






Java Solution






CODE
class Solution {
public int[] twoSum(int[] nums, int target) {

for(int i = 0; i < nums.length; i++) {

for(int j = i + 1; j < nums.length; j++) {

if(nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}

return new int[]{-1, -1};
}
}












Optimizing the Solution



In the brute force approach, we're repeatedly searching for a number that can complete the target sum.



Instead of searching again and again, we can store previously seen numbers in a HashMap.



For every element:




  1. Calculate the required complement.

  2. Check if that complement already exists in the map.

  3. If yes, we've found our answer.

  4. Otherwise, store the current number and its index.



Since HashMap lookups take O(1) on average, we can solve the problem in a single pass.









Optimal Approach (HashMap)






Time & Space Complexity




  • Time Complexity: O(N)

  • Space Complexity: O(N)






Java Solution






CODE
class Solution {
public int[] twoSum(int[] nums, int target) {

HashMap<Integer, Integer> map = new HashMap<>();

for(int i = 0; i < nums.length; i++) {

int complement = target - nums[i];

if(map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}

map.put(nums[i], i);
}

return new int[]{-1, -1};
}
}












Dry Run






Input






CODE
nums = [2,7,11,15]
target = 9
































Index Value Complement Needed Map Before Result
0 2 7 {} Store 2
1 7 2 {2=0} Found 2





Answer






CODE
[0,1]












Key Interview Takeaway



Whenever you hear:




Find a pair whose sum equals a target.




Immediately think:




Can I store previously seen values and look up the required complement in O(1)?




This simple HashMap pattern appears in many interview problems and is worth mastering early.






Follow along as I break down the intuition, brute force, better, and optimal solutions for every problem.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Could A.I. Really Kill All Humans?
1 Quelle
The Next Terrorist Attack Is Predictable
1 Quelle
Anthropic Says It Blocked Possible Efforts to Build Biological Weapons
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Two Sum | HashMap Pattern

Thematisch verwandte Begriffe: HashMap, Pattern · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...