none of the test cases are passing.
Sample output is :
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 3 and 5
But my output is :
1, 4 and 5
2, 3 and 5
1, 2 and 7
1, 3 and 6
none of the test cases are passing.
Sample output is :
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 3 and 5
But my output is :
1, 4 and 5
2, 3 and 5
1, 2 and 7
1, 3 and 6
hi @aakshatmalhotra100
just sort the array in beginning, u will get the o/p in the desired format…
corrected code --> https://ide.codingblocks.com/s/643109
test case 0 is giving TLE
I don’t think it should. Could you please share the question link where you are trying to submit?
question is : Arrays-Target Sum Triplets
myCode: #include
#include
using namespace std;
void arrayInput(int a[], int n) {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
}
void targetSumPair(int a[], int n, int target) {
// using two nested for loops to get all pairs
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
if (a[i] + a[j] + a[k] == target) {
// sort(pairsarr, pairsarr + 3);
cout << a[i] << ", " << a[j] << " and " << a[k] << endl;
}
}
}
}
}
int main() {
int n;
cin >> n;
int a[n];
// take array input
arrayInput(a, n);
sort(a, a+n);
// print target sum pair
int target;
cin >> target;
targetSumPair(a, n, target);
return 0;
}
The time limit is tight, so you need to reduce complexity to O(N^2).
You need to implement something along the lines of this https://www.geeksforgeeks.org/print-all-triplets-with-given-sum/
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.