<p>Given an integer <code>n</code>, return <em>a list of all <strong>simplified</strong> fractions between </em><code>0</code><em> and </em><code>1</code><em> (exclusive) such that the denominator is less-than-or-equal-to </em><code>n</code>. You can return the answer in <strong>any order</strong>.</p>

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

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> [&quot;1/2&quot;]
<strong>Explanation:</strong> &quot;1/2&quot; is the only unique fraction with a denominator less-than-or-equal-to 2.
</pre>

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

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> [&quot;1/2&quot;,&quot;1/3&quot;,&quot;2/3&quot;]
</pre>

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

<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> [&quot;1/2&quot;,&quot;1/3&quot;,&quot;1/4&quot;,&quot;2/3&quot;,&quot;3/4&quot;]
<strong>Explanation:</strong> &quot;2/4&quot; is not a simplified fraction because it can be simplified to &quot;1/2&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
</ul>

// class Solution {
// public:
//     vector<string> simplifiedFractions(int n) {
//         vector<string> res;
//         for(int i=2; i<=n; i++)    
//             for(int j=1; j<i; j++)            
//                 if(gcd(i,j) == 1)   
//                     res.push_back(to_string(j) + "/" + to_string(i));
//         return res;
//     }
// };

// Stern Broccot Tree is a short algorithm in terms of code and slightly more complex to prove. It enumerates the simple fractions in increasing order of denominator size - https://cp-algorithms.com/others/stern_brocot_tree_farey_sequences.html

class Solution {
public:
    vector<string> simplifiedFractions(int n) {
        if (n==1) 
            return {};
        queue<tuple<int, int, int, int>> q;
        vector<string> res;
        q.emplace(0, 1, 1, 1);
        
        while(!q.empty()) {
            auto [num1, den1, num2, den2] = q.front();
            int num = num1 + num2;
            int den = den1 + den2;
            q.pop();
            res.push_back( to_string(num) + "/" + to_string(den) );
            
            if (den + den1 <= n)
                q.emplace(num1,den1,num,den); 
            if (den + den2 <= n)
                q.emplace(num2,den2,num,den); 
        }
        return res;
    }
};



////



////



////




////


29500