The challenge

Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed.

For example, when an array is passed like [19, 5, 42, 2, 77], the output should be 7.

[10, 343445353, 3453445, 3453545353453] should return 3453455.

The solution in C++

Option 1:

1
2
3
4
5
6
7
#include <algorithm>
#include <vector>

long sumTwoSmallestNumbers(std::vector<int> numbers) {
    std::sort(numbers.begin(), numbers.end());
    return (long)numbers[0] + (long)numbers[1];
}

Option 2:

1
2
3
4
5
6
#include <algorithm>

unsigned int sumTwoSmallestNumbers(std::vector<int> numbers) {
  std::nth_element(begin(numbers), next(begin(numbers)), end(numbers));
  return (unsigned int)(numbers[0] + numbers[1]);
}

Option 3:

1
2
3
4
long sumTwoSmallestNumbers(std::vector<int> numbers) {
    std::sort(numbers.begin(), numbers.end());
    return (long long)numbers.at(0) + numbers.at(1);
}

Test cases to validate our solution

 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include <climits>
#include <cstdio>
#include <random>
#include <vector>

Describe(Tests)
{
    It(BasicTest)
    {
        long expected = 13;
        long actual = sumTwoSmallestNumbers({ 5, 8, 12, 19, 22 });
        Assert::That(actual, Is().EqualTo(expected));
    }
    
    It(ExtendedTest1)
    {
        long expected = 6;
        long actual = sumTwoSmallestNumbers({ 15, 28, 4, 2, 43 });
        Assert::That(actual, Is().EqualTo(expected));
    }
    
    It(ExtendedTest2)
    {
        long expected = 10;
        long actual = sumTwoSmallestNumbers({ 3, 87, 45, 12, 7 });
        Assert::That(actual, Is().EqualTo(expected));
    }
    
    It(ExtendedTest3)
    {
        long expected = 4000000000;
        long actual = sumTwoSmallestNumbers({ 2000000000, 2000000000, 2000000000, 2000000000, 2000000000 });
        Assert::That(actual, Is().EqualTo(expected));
    }

    It(ExtendedTest4)
    {
        long expected = 5;
        long actual = sumTwoSmallestNumbers({ 1000, 2, 3 });
        Assert::That(actual, Is().EqualTo(expected));
    }

  
    It(RandomTests)
    {
        auto solution = [](std::vector<int> numbers) {
            std::sort(numbers.begin(), numbers.end());
            return (long)numbers[0] + (long)numbers[1];
        };
        
        std::default_random_engine generator{std::random_device()()};
        std::uniform_int_distribution<int> distributor(1, INT_MAX);
        std::uniform_int_distribution<std::vector<int>::size_type> sizeDistribution(4, 20);
        
        for(int i = 0; i < 100; i++) {
            int length = sizeDistribution(generator);
            
            std::printf("Test for:\n");
            std::vector<int> numbers;
            for(int j = 0; j < length; j++) {
                int n = distributor(generator);
                numbers.push_back(n);
                
                std::printf("%d", n);
                
                if (j != length - 1) {
                    std::printf(", ");
                }
            }
            std::printf("\n<hr>\n");
            long expected = solution(numbers);
            long actual = sumTwoSmallestNumbers(numbers);
            Assert::That(actual, Is().EqualTo(expected));
        }
    }
};