Ad
Permutations
Algorithms
Logic

Given a list of 5 integers and the operators +, -, * and /, what equations will give the minimum and maximum answers?

There is no operator precedence, the equations are simply evaluated from left to right, and each operator can be used only once.

The input is an array of 5 integers, and the output should be a pair of strings showing the order of the operators.

For example, with the input \[1, 2, 3, 4, 5\], the answer should be ["+/-\*","-/_\*"], giving the minimum of (((1 + 2) / 3) - 4) \* 5 = -15, and the maximum of (((1 - 2) / 3) + 4) * 5 = 18.3333.

#include <map>
#include <array>
#include <string>

using namespace std;

double add(double a, double b) { return a + b; }
double sub(double a, double b) { return a - b; }
double mul(double a, double b) { return a * b; }
double div(double a, double b) { return a / b; }

typedef double (*opFunc)(double a, double b);

double calculate(array<int, 5> const numbers, string ops) {
    map<char const, opFunc> const opFuncs = {
       {'+', add}, {'-', sub}, {'*', mul}, {'/', div},
    };
    double result = numbers[0];
    for (int i = 0; i < 4; i++) {
        opFunc op = opFuncs.at(ops[i]);
        result = op(result, numbers[i + 1]);
    }
    return result;
}

pair<string, string> MinMaxEquations(array<int, 5> const& numbers) {

    map<string, double> results;
    string ops = "+-*/";
    sort(ops.begin(), ops.end());
    do {
        results[ops] = calculate(numbers, ops);
    } while (next_permutation(ops.begin(), ops.end()));

    auto [min, max] = minmax_element(results.begin(), results.end(),
        [](const pair<string, double>& p1, const pair<string, double>& p2) {
            return p1.second < p2.second; });
    return make_pair(min->first, max->first);
}