Find non duplicateTriangle Triplets from a list of given numbers I implemented an O(N^2) algorithmic  solution but not sure it is better than my previous implementation Find Triangle Triplets from a list of given numbers
 in case of performance. I adapted almost all suggestions from answer except a structure to keep the triangle triplets.The reasons to ignore the suggestion are follows:


This program added additional feature to remove duplicates from the output
To achieve this additional feature in better way is to keep the data structure as vector instead of  suggested data structure 'structure' in place of vector.
Moreover there is no additional advantage, instead a change in the coding  style  with this suggested change in my perspective


And @nhgrif pointed out in chat my previous solution spitting duplicate triplets and that flaw is taken care here. Let me know is there any other option to make my code better.

#include&lt;vector&gt;
#include&lt;iostream&gt;
#include&lt;algorithm&gt;
#include&lt;set&gt;

bool isTriangleTriplet(const int a, const int b, const int c)
{
    if( ( a + b ) &gt; c &amp;&amp;  ( a + c ) &gt; b &amp;&amp; ( b + c ) &gt; a)
    {
        return true;
    }
    return false;    
}
std::vector&lt;std::vector&lt;int&gt;&gt; findTriangleTriplets( const std::vector&lt;int&gt;&amp; vec_input )
{
    auto size =  vec_input.size();
    std::vector&lt;std::vector&lt;int&gt;&gt;  triplets;
    for( unsigned int i = 0; i &lt; size; ++i )
    {
        for( unsigned int j = i+1; j &lt; size; ++j )
        {
            std::vector&lt;int&gt; temp;
            temp.emplace_back( vec_input.at( i ) );
            temp.emplace_back( vec_input.at( j ) ); 
            triplets.emplace_back( temp );        
    }   }
    std::vector&lt;std::vector&lt;int&gt;&gt;  temp_triplets;

    for(auto &amp;pair : triplets)
    {
        std::vector&lt;int&gt; pers_vec( pair );
        bool flag = false;
        for( auto &amp;num : vec_input )
        {
            if( isTriangleTriplet( pair.at( 0 ), pair.at( 1 ) , num) )
            {           
                if( flag )
                {
                    std::vector&lt;int&gt; temp( pers_vec );
                    temp.emplace_back( num );
                    temp_triplets.push_back( temp );            
                }
                else if( std::count( std::begin( pair ), std::end( pair ), num ) &lt; 2 )
                {
                    pair.emplace_back( num );
                    flag=true;
                }
            }
        }         
    }

    for( auto &amp; x: temp_triplets )
    {
        triplets.emplace_back( x );
    }       
    return triplets;
}

std::set&lt;std::vector&lt;int&gt;&gt; SieveDuplicates( std::vector&lt;std::vector&lt;int&gt;&gt; &amp; triplets )
{
    std::set&lt;std::vector&lt;int&gt;&gt; sieved_triplets;
    for( auto &amp;x : triplets )
    {
        std::sort( std::begin( x ), std::end( x ) );
        sieved_triplets.emplace( x ); 
    }
    return sieved_triplets;
}

int main()
{   
    const std::vector&lt;int&gt; vec_input { 2, 3, 4, 1, 5, 6};
    std::vector&lt;std::vector&lt;int&gt;&gt;  triplets = findTriangleTriplets( vec_input );
    std::cout&lt;&lt; "Triplet size:" &lt;&lt; triplets.size()&lt;&lt;"\n";
    for ( auto &amp;x : triplets)
    {
        for( auto &amp;y : x )
        {            
           std::cout&lt;&lt;y&lt;&lt;" ";
        }
        std::cout&lt;&lt;"\n";        
    }

    std::set&lt;std::vector&lt;int&gt;&gt; sieved_triplets=SieveDuplicates( triplets );
    std::cout&lt;&lt; "Sieved Triplet size:" &lt;&lt; sieved_triplets.size()&lt;&lt;"\n";
    for ( auto &amp;x : sieved_triplets )
    {
        for( auto &amp;y : x )
        {            
           std::cout&lt;&lt;y&lt;&lt;" ";
        }
        std::cout&lt;&lt;"\n";        
    }
}
Not an O(N^2) algorithm

You create a vector that contains every (i,j) pair.  This vector contains O(N^2) elements.

For each pair, you loop across the original vector of N elements.


Outer loop = O(N^2)
Inner loop = O(N)
Total = O(N^3)
By the way, unless you allow printing ranges of answers, there is no O(N^2) solution to this problem because there can be O(N^3) triplets that need to be printed.