Created
November 23, 2018 17:43
-
-
Save WennderSantos/d9a8bd845b9cee761093cdbf72ed8738 to your computer and use it in GitHub Desktop.
Find pairs that sum to eight. O(n)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var input = new int[]{4, 5, 2, 3, 4, 1}; | |
var memory = new Dictionary<int, int>(); | |
var results = new List<(int idx1, int idx2)>(); | |
for (var i = 0; i < input.Length; i++) | |
{ | |
if (memory.ContainsKey(input[i])) | |
results.Add((memory[input[i]], i)); | |
else | |
memory.Add(8 - input[i], i); | |
} | |
foreach(var (idx1, idx2) in results) | |
Console.WriteLine($"Sum of {input[idx1]} and {input[idx2]} is 8"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment