-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquad_TuplewithSum_K.cpp
More file actions
35 lines (30 loc) · 889 Bytes
/
Copy pathquad_TuplewithSum_K.cpp
File metadata and controls
35 lines (30 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <algorithm>
#include <iostream>
using namespace std;
void quadTupleWithSumk(int arr[],int size,int k){
sort(arr,arr+size);
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
int remsum = k - (arr[i] + arr[j]);
int low = j + 1,high = size -1;
while(low < high){
if(arr[low] + arr[high] < remsum) low++;
else if (arr[low] + arr[high] > remsum) high--;
else
{
cout << "(" << arr[i] << " " << arr[j] << " " <<
arr[low] << " " << arr[high] << ")\n";
low++, high--;
}
}
}
}
}
int main()
{
int arr[] = { 2, 7, 4, 0, 9, 5, 1, 3 };
int sum = 20;
int size= (*(&arr + 1) - arr);
quadTupleWithSumk(arr,size,sum);
return 0;
}