<p>You are given an integer array <code>cards</code> of length <code>4</code>. You have four cards, each containing a number in the range <code>[1, 9]</code>. You should arrange the numbers on these cards in a mathematical expression using the operators <code>[&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;]</code> and the parentheses <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code> to get the value 24.</p>

<p>You are restricted with the following rules:</p>

<ul>
	<li>The division operator <code>&#39;/&#39;</code> represents real division, not integer division.

	<ul>
		<li>For example, <code>4 / (1 - 2 / 3) = 4 / (1 / 3) = 12</code>.</li>
	</ul>
	</li>
	<li>Every operation done is between two numbers. In particular, we cannot use <code>&#39;-&#39;</code> as a unary operator.
	<ul>
		<li>For example, if <code>cards = [1, 1, 1, 1]</code>, the expression <code>&quot;-1 - 1 - 1 - 1&quot;</code> is <strong>not allowed</strong>.</li>
	</ul>
	</li>
	<li>You cannot concatenate numbers together
	<ul>
		<li>For example, if <code>cards = [1, 2, 1, 2]</code>, the expression <code>&quot;12 + 12&quot;</code> is not valid.</li>
	</ul>
	</li>
</ul>

<p>Return <code>true</code> if you can get such expression that evaluates to <code>24</code>, and <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> cards = [4,1,8,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> (8-4) * (7-1) = 24
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cards = [1,2,1,2]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>cards.length == 4</code></li>
	<li><code>1 &lt;= cards[i] &lt;= 9</code></li>
</ul>

class Solution {
    static constexpr double target = 24;
    static constexpr auto calculate = [](auto a, auto b) {
        auto results = std::array{ a + b, a - b, b - a, a * b, DBL_MAX, DBL_MAX};
        if (a) {
            results[4]= b / a;
        }
        if (b) {
            results[5] = a / b;
        }

        return results;
    };

public:
    bool judgePoint24(vector<int>& cards) {
        std::vector<double> c(std::begin(cards), std::end(cards));
        return backtracking(c, c.size());
    }
    
    bool backtracking(vector<double>& cards, int active_cards) {
        if (active_cards == 1) {
            const auto card = *std::ranges::find_if(
                cards, [](const auto v) { return v != DBL_MAX; });
            return std::abs(card - target) <= 0.1;
        }

        for (int i = 0; i < cards.size(); ++i) {
            if (cards[i] == DBL_MAX)
                continue;
            for (int j = i + 1; j < cards.size(); ++j) {
                if (cards[j] == DBL_MAX)
                    continue;

                const auto card_i = cards[i], card_j = cards[j];
                cards[i] = cards[j] = DBL_MAX;

                for (const auto result : calculate(card_i, card_j)) {
                    if(std::abs(result - DBL_MAX) <= 0.1) continue;
                    cards[i] = result;
                    if (backtracking(cards, active_cards - 1)) {
                        return true;
                    }
                }
                cards[i] = card_i, cards[j] = card_j;
            }
        }
        return false;
    }
};

10168