#include <bits/stdc++.h>
using namespace std;

void findelements(int *a, int n, int K)
{
    // sort the array using default sort library function, O(logn) generally
    sort(a, a + n);
    // run the first loop
    for (int k = 0; k < n; k++)
    {
        // i=k+1 & j=n-1 (initialized) ; run till i<j
        for (int i = k + 1, j = n - 1; i < j;)
        {
            if (a[i] + a[j] + a[k] == K)
            { // if sum ==K
                // print & return
                printf("three elements are found to sum to %d. These are %d,%d,%d\n", K, a[k], a[i], a[j]);
                return;
            }
            // if sum <K increment i since array is sorted & we need higher value elements
            else if (a[i] + a[j] + a[k] < K)
                i = i + 1;
            else
                j = j - 1; //  if sum >K decrement j since array is sorted & we need lower value elements
        }
    }

    cout << "no such three elements can be found" << endl; // no such element trio found
    return;
}

int main()
{
    int K, count = 0, n;
    // enter array length
    cout << "enter size of elements\n";
    cin >> n;
    int *a = (int *)(malloc(sizeof(int) * n));
    cout << "enter elements\n"; // fill the array
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    cout << "enter the sum, K " << endl;
    cin >> K;
    findelements(a, n, K);

    return 0;
}

Output

enter size of elements
5
enter elements
1  
2
3
4
5
enter the sum, K
6
three elements are found to sum to 6. These are 1,2,3