i had done this with traditional method still getting an error,is the error is about the time complexity nd is its time complexity is O(n^3)
Arrays-Target Sum Triplets
Piyush, you are getting an error, because the order of output you are getting through your code is different from the one given in the sample test case…
Basic approach u need to follow is that take three loops, i.e
run i=0 to n-2
run j=i+1 to n-1
run k=j+1 to n
and then simply check,
if(ar[i]+ar[j]+ar[k]==target)
then :
cout<<ar[i]<<","<<" “<<ar[j]<<” “<<“and”<<” "<<ar[k];
cout<<endl;
But make sure that before you call this function, sort the array, so that the output comes in the required format as given in test cases…
ok but please explain why i<n-2 and j<n-1 and the complexity of my code and how u calculate it
This is because you need to calculate triplets, that means 3 nos on consecutive order, so instead of all loops going uptil n, you can take three values ending at less than n-2,n-1 and n, thts why outer loop will go from 0 to less than n-2, then from 0 to less than n-1 and last will be from 0 to less than n…
This is the order you need to follow.