-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter16.cs
More file actions
29 lines (26 loc) · 794 Bytes
/
Copy pathChapter16.cs
File metadata and controls
29 lines (26 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
namespace CodingProblems
{
public class Chapter16
{
public List<Tuple<int, int>> SumPairs(int[] array, int sum)
{
List<Tuple<int, int>> results = new List<Tuple<int, int>>();
IDictionary<int, int> knownValues = new Dictionary<int, int>();
for (int i = 0; i < array.Length; i++)
{
var complement = sum - array[i];
if (knownValues.ContainsKey(complement))
{
results.Add(new Tuple<int, int>(knownValues[complement], i));
}
else
{
knownValues[array[i]] = i;
}
}
return results;
}
}
}