Given [1, 2, 3, 4, 5] and target 8, print the two indices (0-based) separated by space.
Toolliyo Coach
Progressive help: Nudge → Guide → Approach. Full solution stays behind the Solution tab.
Editor is open — no login wall to practice.
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] nums = { 1, 2, 3, 4, 5 };
int target = 8;
var map = new Dictionary<int, int>();
for (int j = 0; j < nums.Length; j++) {
int need = target - nums[j];
if (map.ContainsKey(need)) { Console.WriteLine(map[need] + " " + j); return; }
map[nums[j]] = j;
}
}
}
Prefer Coach (Nudge → Guide → Approach) before revealing. No forced signup.
Classic hash-map two sum — O(n) time.
Sign in to save and review your submission history. You can still Run code on the Editor tab as a guest.
Sign in