How to Sum the Two Lowest Positive Integers in C++


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:

#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:

#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:

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

#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));
        }
    }
};