My code is giving wrong answer please hep

Your code fails when there is duplicate no in array.Suppose arr is [3 1 4 3 6 8] and target is 9 then it fails to produce correct o/p as 1 and 8 , 3 and 6. It gives 1 and 8 , 3 and 6 , 3 and 6 as o/p.

please help me what to do?

@Ariba_Parveen i checked your input and it is giving required output

yes but testcase #4 is not passing please help my code is correct

Your code isn’t giving correct o/p in case:-
arr=5 2 3 4 1
target=6
then o/p should be 1 and 5 , 2 and 4
you code o/p is : 1 and 5 , 2 and 4 ,3 and 3.

If you are not able to solve the problem then try this approach -:
First sort the array then take i and j pointing to the start and end respectively. While i<j check the conditions , if you find the match i.e arr[i]+arr[j]==target print it and increment both i and j , else if arr[i]+arr[j]<target increment i , else increment j.

If arr[i]+arr[j]==target then increment i and decrement j , else if arr[i]+arr[j]<target then increment i else decrement j.

thanks for your logic …I want to ask is there no way to change some part of my code and get the required output…
and also i want to ask what if arr[i]+arr[j]>target in that case what do we do?


please check I did what the way you told me to do and this time and two test case passed …
I am loosing all interest in coding…please help
your logic is not correct for different type of input

if arr[i]+arr[j]>target then you need to decrease the value of j . I have made small change in your code.

You aren’t decreasing the value of j when arr[i]+arr[j]>target.