4th test case is not running

#include
#include
using namespace std ;
int main()
{
int n ;
int a[n] ;
cin >> n ;

for(int i=0;i<n;i++)
{
    cin >>a[i] ;
}            

sort(a,a+n);

int target ;
cin >> target ;

int j=n-1 ;
int i=0 ;
while(i<=j)
{
 int s =a[i]+a[j];
    if(s==target)
     {
         cout <<a[i]<<" and "<<a[j]<<endl;
		 i++ ;
		 j-- ;
     }
     else{ if(s>target)
     j-- ;
     else
     i++ ;
	 }
}

} //// 4th testcase is not running when submitting the code …

Hi @3lokesharora, consider the following test case:
5
1 4 5 6 9
10
where 10 is the target. Your code is also giving 5 5 as an output which is not correct. You cannot add the same number twice to produce the target sum,

Modify your code such that one value is used only once.

If your query gets resolved, mark it as resolved, otherwise reply to this thread.

1 Like