Two Sum Indices
Ever had a list of numbers and wanted to find two that add up to a specific target? That’s exactly what Two Sum Indices is about! You’re given an array of integers and a target value. Your mission is to find the indices of the two numbers that correctly add up to that total.
The "secret sauce" here is a Hash Table. While you could use nested loops to check every pair (which is slow), a hash table lets you ask: "Do I have the companion number that, when added to this one, equals our target?" as you walk through the array. This clever trick means you only scan the list once, giving you a lightning-fast O(n) linear complexity! This challenge is a classic because it perfectly demonstrates how to trade a tiny bit of memory for incredible speed. It’s the absolute best way to solve pairing problems with maximum elegance!
Keep the algorithm focused on one clear invariant and update path so correctness is easy to verify from left to right. This reduces accidental branching errors and helps ensure the final output stays consistent with the problem contract across random and adversarial test shapes.
Examples
2 + 7 = 9.
2 + 4 = 6.
Both values 3 are used.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public int[] two_sum_indices(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int need = target - nums[i];
if (seen.containsKey(need)) return new int[]{seen.get(need), i};
seen.put(nums[i], i);
}
return new int[]{};
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
