Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bkb181/ada2120e170a58981459e63def3a0772 to your computer and use it in GitHub Desktop.
Save bkb181/ada2120e170a58981459e63def3a0772 to your computer and use it in GitHub Desktop.
vector<vector<int>> four_element(int arr[], int n, int key)
{
int i, j, k=0;
int m= (n *(n-1))/2;
triplet pair[m];
vector<vector<int>> res;
for(i= 0; i<n-1; i++)
{
for(j= i+1; j< n; j++)
{
pair[k].first= i;
pair[k].second= j;
pair[k].sum = arr[i]+arr[j];
k++;
}
}
sort(pair, m, sizeof(pair[0]), comp);
i = 0;
j = m-1;
while(i<m&&j >= 0)
{
if((pair[i].sum+pair[j].sum==key)&&check_common(pair[i], pair[j]))
res.insert({arr[pair[i].first],arr[pair[i].second], arr[pair[j].first], arr[pair[j].second]});
else if(pair[i].sum+pair[j].sum <key)
i++;
else
j--;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment