using System.Collections.Generic;
public static void Main()
var result = Solution.TwoSum(new []{7,11,3,23,22,1,2}, 3);
var leftIndex = result[0];
var rightIndex = result[1];
Console.WriteLine("{0} {1}", leftIndex, rightIndex);
private static int[]? CheckValue(
IEnumerable<(int, bool, int)> comparable,
var (value, equals, index) = source;
return (comparable.Where(v => (value + v.Item1) == target).FirstOrDefault()) switch
var (_, _, addIndex) => new []{ index, addIndex}
private static int[] FindSolution(IReadOnlyList<(int, bool, int)> values, int target)
for (var i = 0; i < values.Count; i++)
var result = CheckValue(values[i], values.Skip(i+1), target);
if(result == null) continue;
throw new Exception("No values matched expected conditions.");
public static int[] TwoSum(int[] nums, int target)
.Select((v, i) => (v, v > target, v == target, i))
.Where(vTuple => vTuple switch
(_, true, _, _) => false,
}).Select(x => (x.Item1, x.Item3, x.Item4)).ToList();
if(validMembers.Count == 0) throw new Exception("No members in provided set can equal the target value");
var targetInSet = validMembers.Where(x => x.Item2 == true);
if (targetInSet.Count() > 0)
return new int[] { -1, targetInSet.First().Item3 };
else return FindSolution(validMembers, target);