Skip to content

Instantly share code, notes, and snippets.

@ff8c00
Created July 10, 2017 20:28
Reddit Daily Programmer 323 Easy
using System;
using System.Collections.Generic;
using System.Linq;
namespace Reddit
{
internal static class Easy
{
internal static void Run(string input)
{
var items = input
.Split(' ')
.Select(e => int.Parse(e))
.ToArray();
Run(items);
}
internal static void Run(int[] input)
{
Array.Sort(input);
var items = new HashSet<Tuple<int, int, int>>();
int count = input.Length;
for (int i = 0; i < count - 2; i++)
{
int a = input[i];
int start = i + 1;
int end = count - 1;
while (start < end)
{
int b = input[start];
int c = input[end];
int value = a + b + c;
if (value == 0)
{
var item = new Tuple<int, int, int>(a, b, c);
if (!items.Contains(item))
{
items.Add(item);
}
end = end - 1;
}
else
{
if (value > 0)
{
end = end - 1;
}
else
{
start = start + 1;
}
}
}
}
foreach (var item in items)
{
Console.WriteLine("{0} {1} {2}",
item.Item1, item.Item2, item.Item3);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment