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<=T<=100
1<=N<=100
Example: Input: 1 6 1 5 1 0 6 0 7
Output: 1
Here is the code
#include <iostream>
#include <cassert>
#include <vector>
int main() {
int t, n, val, x, sum = 0;
bool found = false;
std::cin >> t;
assert(t >= 1 && t <= 100);
while (t--)
{
std::cin >> n;
assert(n >=1 && n <= 100);
std::vector<int> vec(n);
for (int i = 0; i < n; i++)
{
std::cin >> val;
vec[i] = val;
}
std::cin >> x;
for (int i = 0; i < n - 3; i++)
{
for (int j = i + 1; j < n - 2; j++)
{
for (int k = j + 1; k < n - 1; k++)
{
for (int l = k + 1; l < n; l++)
{
sum = vec[i] + vec[j] + vec[k] + vec[l];
if (sum == x)
{
found = true;
break;
}
}
}
}
}
if (found == true)
{
std::cout << 1 << std::endl;
}
else
{
std::cout << 0 << std::endl;
}
}
return 0;
}
How to optimise this code?