Code running slow

The is running absolutely fine if I am providing custom inputs but when I submit it rejects it. I have nearly given one day on this single problem.
Please suggest what can be done.
Here goes my code::
#include
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[1000];
int n, target, sum=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cin>>target;
int i=0;
int l=1;
int r=n-1;
sort(a, a+n);
for(i=0;i<n;i++)
{
while(l<r)
{
//cout<<“I entered while loop”<<endl;
sum = a[i]+a[l]+a[r];
cout<<sum<<endl;
if(sum == target)
{
cout<<a[i]<<", “<<a[l]<<” and "<<a[r]<<endl;
l+=1;
r-=1;
//cout<<“i am in sum == target”<<endl;
}
else if(sum > target)
{
r-=1;
//cout<<“i am checking sum>target”<<endl;
}

            /*if((l+1==r) || (l>r))
            {
                i+=1;
                r=n-1;
                l=i+1;
                cout<<"I am checking l == r"<<endl;
            }*/

// r-=1;
// l+=1;
// i+=1;
// cout<<“i decrement r”<<endl;
}
i++;
r=n-1;
l=i+1;
}

return 0;

}

with Regards
Yash

even for the custom input
9
5 7 9 1 2 4 6 8 3
10
you are getting wrong output
1, 2 and 7
1, 3 and 6
1, 4 and 5
3, 3 and 4

you need to Print only unique triplets.you missed 2 3 and 5 too

1.Sort the element in ascending order

  1. Start a loop from i=0 to i<n. We mark arr[i] at each instance as a fixed element for that iteration and work to find a pair of elements such that their sum is equal to target-arr[i] hence ensuring that the net sum of the three elements would be equal to target.
  2. Inside this loop, implement the two pointer approach. Keep a left pointer starting from i+1 and a right pointer from n-1.
  3. Work an inner left till left<right. For each iteration , check whether a[i] + a[left] + a[right] == target. If so ,print the triplet. Else if this sum = a[i] + a[left] + a[right] is less than the target , then increment the left pointer by one. Else decrement the right pointer by one.

3.Thus, all the triplets have been printed.

you can see this


if this solves your doubt please mark it as resolved :slight_smile:

ma’am I have coded this problem again for identical triplets, but now also it is showing the wrong answers. What can I do now. Please guide.

hello @atreyyash

pls share ur updated code with me

#include #include<bits/stdc++.h> using namespace std; int main() { int a[1000]; int n, target, sum=0; cin>>n; for(int i=0;i<n;i++) { cin>>a[i]; } cin>>target; int i=0; int l=1; int r=n-1; sort(a, a+n); for(i=0;i<n-2;) { while(l<r) { sum = a[i]+a[l]+a[r]; if(sum == target) { cout<<a[i]<<", “<<a[l]<<” and "<<a[r]<<endl; l+=1; r-=1; } else if(sum > target) { r-=1; } else if(sum<target) { i++; } } i+=1; r=n-1; l=i+1; } return 0; }

go to this link ->https://ide.codingblocks.com/

save ur code , a link will be generated in ur search bar
share that link with me

I think this will help you

check now->

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.