Coding Challenge Solution - Good Range This my solution to this Good Range Coding Challenge

There is a number space given from 1 to N. And there are M queries followed by that. In each query, we were given a number between 1 to N (both inclusive). We add these number one by one into a set.
Good range: A range in which there is exactly one element present from the set.
For each query, we need to find the good ranges. We need to return the sum of boundry of all good ranges.
Input:
First line will take two integer for input N and M.
Then following m lines would be numbers between 1 and N (both inclusive).
Output:
Following M lines contains sum of boudaries of good ranges.
Note:
Range can consist of single element and represented as (x-x) where boundary sum will be x+x.
Example:
Input:

10 4 
2 
5 
7 
9 


Output:

11 
18 
30 
46 


Explaination:

step-1) set: 2 
good range: (1-10) 
sum: 1+10=11 

step-2) set: 2 5 
good range: (1-4), (3-10) 
sum: 1+4+3+10=18 

step-3) set: 2 5 7 
good range: (1-4), (3-6), (6-10) 
sum: 1+4+3+6+6+10=30 

step-4) set: 2 5 7 9 
good range: (1-4), (3-6), (6-8), (8-10) 
sum: 1+4+3+6+6+8+8+10=46

#include &lt;iostream&gt;
#include &lt;set&gt;
using namespace std;

class Solution
{
    public: 
    Solution(const unsigned int _N, const unsigned int _M) : N(_N), M(_M) {}
    
    
    void solve()
    {
        for(unsigned int i=0; i&lt;M; ++i)
        {
            unsigned int v; 
            cin &gt;&gt; v; 
            cout &lt;&lt; &quot;New Element = &quot; &lt;&lt; v &lt;&lt; endl; 
            q.insert(v); 
            print_res(); 
            cout &lt;&lt; endl; 
        }
    }
    
    void print_res()
    {        
        unsigned int left=1;
        auto it=q.begin(); 
        unsigned int last = *it; 

        for(++it; it!=q.end(); ++it)
        {
            const unsigned int curr = *it; 
            const unsigned int right = curr-1; 
            cout &lt;&lt; &quot;[&quot; &lt;&lt; left &lt;&lt; &quot;, &quot;&lt;&lt; right &lt;&lt; &quot;] contains &quot; &lt;&lt; last &lt;&lt; &quot; and sum = &quot; &lt;&lt; (left+right) &lt;&lt; endl; 
            left = last+1; 
            last = curr; 
        }
        const unsigned right = N; 
        cout &lt;&lt; &quot;[&quot; &lt;&lt; left &lt;&lt; &quot;, &quot;&lt;&lt; right &lt;&lt; &quot;] contains &quot; &lt;&lt; last &lt;&lt; &quot; and sum = &quot; &lt;&lt; (left+right) &lt;&lt; endl; 
    }
    
    private: 
    unsigned int N; 
    unsigned int M; 
    set&lt;unsigned int&gt; q; 
}; 


int main() {
    // your code goes here
    unsigned int N=0; 
    unsigned int M=0; 
    cin &gt;&gt; N &gt;&gt; M; 
    Solution sol(N,M); 
    sol.solve(); 
    return 0;
}


Note: I am aware I'm returning more information than required by the problem description but I have chosen to do it to include also debugging information
Don't force a class where a function is perfectly fine.
The namespace std is not designed for wholesale importation, see "Why is “using namespace std” considered bad practice?" for more detail.
Identifiers starting with an underscore followed by an uppercase letter are reserved.
Input is generally unreliable. But you don't test for error at all.
Always recomputing the current result from first principles, instead of just updating it with the newest addition, is a waste of time.
Flushing is expensive, so avoid std::endl and explicitly request it with std::flush where unavoidable.
You should probably follow the requested output-format...
You know that int in unsigned int is implicit?
In C++ and C99+, return 0; is implicit for main().
I wonder why you sometimes surround binary operators with space, and sometimes don't.
Having over-long lines is very cumbersome to read. Admittedly, you need not make a hard cut at 79 nowadays.
You should double-check whether duplicate inputs can occurr (there are none in the example), and if so whether they should cause anything but repeated output.
I assumed duplicated possible, and they only cause output.


Duplicates have an effect would destroy ranges instead of adding them, thus needing an ordered std::set.
Duplicates not possible would mean the std::unordered_set can be dispensed with.



#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;unordered_set&gt;

int main() {
    unsigned n, m, x;
    std::cin &gt;&gt; n &gt;&gt; m;
    if (!std::cin || !n) {
        std::cerr &lt;&lt; "Bad Input!\n";
        return EXIT_FAILURE;
    }
    std::unordered_set&lt;unsigned&gt; nums;
    auto r = 0ULL;
    while (--m) {
        std::cin &gt;&gt; x;
        if (!std::cin || x &lt; 1 || x &gt; n) {
            std::cerr &lt;&lt; "Bad Input!\n";
            return EXIT_FAILURE;
        }
        auto [iter, created] = nums.insert(x);
        if (created)
            r += 2 * x;
        std::cout &lt;&lt; r &lt;&lt; '\n';
    }
}