Four Elements from GeeksForGeeks This is a question from GeeksForGeeks

Given an array of integers, find a combination of four elements in the
array whose sum is equal to a given value X.
Input: First line consists of T test cases. First line of every test
case consists of an integer N, denoting the number of elements in an
array. Second line consists of N spaced array elements. Third line of
every test case X.
Output: Single line output, print 1 if combination is found else 0.
Constraints: 1&lt;=T&lt;=100
1&lt;=N&lt;=100
Example: Input: 1 6 1 5 1 0 6 0 7
Output: 1

Here is the code
#include &lt;iostream&gt;
#include &lt;cassert&gt;
#include &lt;vector&gt;

int main() {
    int t, n, val, x, sum = 0;
    bool found = false;
    std::cin &gt;&gt; t;
    assert(t &gt;= 1 &amp;&amp; t &lt;= 100);

    while (t--)
    {
        std::cin &gt;&gt; n;
        assert(n &gt;=1 &amp;&amp; n &lt;= 100);
        std::vector&lt;int&gt; vec(n);
        for (int i = 0; i &lt; n; i++)
        {
            std::cin &gt;&gt; val;
            vec[i] = val;
        }

        std::cin &gt;&gt; x;

        for (int i = 0; i &lt; n - 3; i++)
        {
            for (int j = i + 1; j &lt; n - 2; j++)
            {
                for (int k = j + 1; k &lt; n - 1; k++)
                {
                    for (int l = k + 1; l &lt; n; l++)
                    {
                        sum = vec[i] + vec[j] + vec[k] + vec[l];
                        if (sum == x)
                        {
                            found = true;
                            break;
                        }
                    }
                }
            }
        }

        if (found == true)
        {
            std::cout &lt;&lt; 1 &lt;&lt; std::endl;
        }
        else
        {
            std::cout &lt;&lt; 0 &lt;&lt; std::endl;
        }
    }
    return 0;
}

How to optimise this code?
When to use Assert
Generally assert() is a debugging tool. The GeekforGeeks website itself warns against using it in certain cases.

If the NODEBUG compilation flag is used in the file the assertion is removed.

It might be better to use if statements and write the output to std::cerr. That would provide a much more meaningful error message.

Reduce Complexity, Follow SRP
The Single Responsibility Principle states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.


  Robert C. Martin expresses the principle as follows:
          A class should have only one reason to change.


While this is primarily targeted at classes in object oriented languages it applies to functions and subroutines well.

The more separate functions there are the easier it is to understand or read the code. This also makes it easier for any programmer to maintain or debug the code.

The main() function could be broken up into at multiple functions. The main() function should generally just control the program and handle any exceptions that get to that point.

An example of a decent main() function for this program might be:

int main() {
    int t;
    std::cin &gt;&gt; t;
    assert(t &gt;= 1 &amp;&amp; t &lt;= 100);
    int x;

    while (t--)
    {
        std::vector&lt;int&gt; vec = getTestCase(x);

        std::cout &lt;&lt; ((executeTestCase(vec, x)) ? 1 : 0) &lt;&lt; std::endl;
    }

    return 0;
}


The first function that might be of use is an test case input function.

std::vector&lt;int&gt; getTestCase(int &amp;x)
{
    int n;
    std::cin &gt;&gt; n;
    assert(n &gt;= 1 &amp;&amp; n &lt;= 100);
    std::vector&lt;int&gt; vec(n);
    for (int i = 0; i &lt; n; i++)
    {
        int val;
        std::cin &gt;&gt; val;
        vec.push_back(val);
    }    

    std::cin &gt;&gt; x;

    return vec;
}


The second function that might be of use would be a that finds if there are values that meet the proper conditions.

bool executeTestCase(std::vector&lt;int&gt; vec, int x)
{
    bool found = false;
    int sum = 0;
    int n = vec.size();

    for (int i = 0; i &lt; n - 3; i++)
    {
        for (int j = i + 1; j &lt; n - 2; j++)
        {
            for (int k = j + 1; k &lt; n - 1; k++)
            {
                for (int l = k + 1; l &lt; n; l++)
                {
                    sum = vec[i] + vec[j] + vec[k] + vec[l];
                    if (sum == x)
                    {
                        found = true;
                        break;
                    }
                }
            }
        }
    }    

    return found;
}


Improve Performance
Since the code is using a C++ container class, it might be possible to use iterators to improve performance. Once the code is broken up into functions and compiled -O3 it can be profiled to find where any bottlenecks exist.