Compiler says TLE

#include
#include

using namespace std;

int main()
{

int n, key;
cin >> n;

int a[1000];

for (int x = 0; x < n; x++)
{
    cin >> a[x];
}
cin >> key;
std::sort(a, a + n);

for (int i = 0; i < n - 1; i++)
{
    for (int j = i + 1; j < n; j++)
    {

        for (int k = j + 1; k < n; k++)
        {
            if (a[i] + a[j] + a[k] == key)
            {
                cout << a[i] << ", " << a[j] << " and " << a[k] << endl;
            }
        }
    }
}

return 0;

}

This code works completely well in my IDE but codeblocks says I should use another method other than brute force. I did not understand that.

yes this is brute force approach
time complexity of your code is high

you have to optimise you code

check this code approach

I haven’t learnt verctors so I was not able to understand the later part of the code.

But i think i got the logic thanks for helping out.

your code gives output as 2,4 and 4. Which is not acceptable, just now saw that. Please give correct explanation instead of sending direct solution.

Why you think so ??
for this input
10
5 7 9 1 2 4 6 3 4 5
10
Correct output is
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 3 and 5
2, 4 and 4

i didn’t use vector in this code
about which code you are taking?

Logic:
first i sort the vector after that

  1. select one element arr[i] and then use two pointer approach to find other 2 elements
    such that sum of these 2 elements = target - arr[i];
  2. there is one problem here that we may have duplicate ans
    so to avoid that i have used set which stores only unique elments so automatically remove the duplicate
    and at end we are just printing the elements of set using for each loop

i hope now it’s clear to you

user photo logical coder a day ago satyam09.sm: your code gives output as 2,4 and 4. Which is not acceptable, Why you think so ?? for this input 10 5 7 9 1 2 4 6 3 4 5 10 Correct output is 1, 2 and 7 1, 3 and 6 1, 4 and 5 2, 3 and 5 2, 4 and 4 …no sir its sample out has ans only upto 2, 3 and 5…since there are no duplicates…thats incorrect…

check correctly
my input is different from sample input given in question

duplicates means
both ans are exactly same
that doesn’t happen here