1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 19:19:07 +01:00
CodeWars/7kyu/sum_of_two_lowest_positive_integers/solution.cs

27 lines
627 B
C#
Raw Normal View History

public static class Kata
{
public static int sumTwoSmallestNumbers(int[] numbers)
{
var smallest = new int[] {numbers[0], numbers[1]};
if (smallest[0] > smallest[1]) {
var temp = smallest[0];
smallest[0] = smallest[1];
smallest[1] = temp;
}
for (var i = 2; i < numbers.GetLength(0); i++) {
var number = numbers[i];
if (number < 0) continue;
else if (number < smallest[0]) {
smallest[1] = smallest[0];
smallest[0] = number;
} else if (number < smallest[1]) {
smallest[1] = number;
}
}
return smallest[0] + smallest[1];
}
}