Challenge: Two Sum

A classic interview warm-up. Implement TwoSum then click Validate — your code is compiled and tested live.

The problem
  • Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target.
  • Each input has exactly one solution, and you may not use the same element twice.
  • The two indices may be returned in any order.
  • Example: nums = [2, 7, 11, 15], target = 9 → [0, 1] (because 2 + 7 = 9).
Hints (click to show)
  1. The brute-force solution uses two nested loops — O(n²).
  2. For O(n), keep a Dictionary<int, int> mapping each value to its index.
  3. For each number, check whether target - num is already in the dictionary.