Bird
0
0
DSA Cprogramming~10 mins

Two Sum Problem Classic Hash Solution in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Two Sum Problem Classic Hash Solution
Start with empty hash map
For each number in array
Calculate complement = target - current number
Check if complement in hash map?
YesReturn indices
No
Add current number and index to hash map
Move to next number
If end reached without match
Return no solution
We scan the array once, storing numbers and their indices in a hash map. For each number, we check if its complement to reach the target exists in the map. If yes, we return the pair.
Execution Sample
DSA C
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
  // hash map stores number -> index
  // This is a placeholder; actual hash map implementation needed
  for (int i = 0; i < numsSize; i++) {
    int complement = target - nums[i];
    if (hash_map_contains(complement)) {
      *returnSize = 2;
      int* result = malloc(2 * sizeof(int));
      result[0] = hash_map_get(complement);
      result[1] = i;
      return result;
    }
    hash_map_add(nums[i], i);
  }
  *returnSize = 0;
  return NULL;
}
This code finds two numbers in the array that add up to the target by using a hash map for quick lookups.
Execution Table
StepOperationCurrent Number (nums[i])Complement (target - nums[i])Hash Map State (number:index)ActionResult / Visual State
1Start--{}Initialize empty hash mapHash map empty
2Process nums[0]27{}Check complement 7 in map? NoAdd 2:0 to map -> {2:0}
3Process nums[1]72{2:0}Check complement 2 in map? YesFound pair (0,1) because 2 + 7 = 9
4Return--{2:0}Return indices (0,1)Solution found, stop
💡 At step 3, complement 2 found in hash map, so pair (0,1) returned.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
i-011
nums[i]-277
complement-722
hash_map{}{2:0}{2:0}{2:0}
Key Moments - 2 Insights
Why do we check for the complement in the hash map before adding the current number?
Because if we add the current number first, we might find the same number as complement and incorrectly pair it with itself. Checking first ensures we only pair different indices. See step 3 in execution_table.
What happens if no two numbers add up to the target?
The loop finishes without finding a complement in the hash map, so the function returns no solution. This is shown by the exit_note after all steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the hash map state after processing nums[0]?
A{}
B{2:0}
C{7:0}
D{2:1}
💡 Hint
Check the 'Hash Map State' column at step 2 in execution_table.
At which step does the algorithm find the solution?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step where complement is found in the hash map in execution_table.
If the target was 10 instead of 9, what would happen at step 3?
AComplement found, solution returned
BAlgorithm stops immediately
CComplement not found, current number added to map
DHash map cleared
💡 Hint
Think about complement calculation and hash map lookup at step 3 in execution_table.
Concept Snapshot
Two Sum Classic Hash Solution:
- Use a hash map to store number:index
- For each number, compute complement = target - number
- Check if complement exists in map
- If yes, return indices
- Else, add current number to map
- Runs in O(n) time with O(n) space
Full Transcript
The Two Sum problem is solved by scanning the array once and using a hash map to remember numbers and their indices. For each number, we calculate the complement needed to reach the target. We check if this complement is already in the hash map. If yes, we return the pair of indices immediately. If not, we add the current number and its index to the hash map and continue. This approach ensures we find the solution in one pass efficiently.