Arrays- Target sum pairs only second case is correct

Why are other cases wrong?
https://ide.codingblocks.com/s/62119

Hey Divyam, your code is not giving output in increasing order like 2 and 3… but it guves output 3 and 2.
so first sort the array and then find the answer

5
1 2 3 4 5
5
o/p is ; 1 and 4
2 and 3

Please can you make changes in the program

Hi Divyam, you just had to sort the input array. I’ve done it here and the code is working for all test cases now. https://ide.codingblocks.com/s/62395

Thank you!
Is there a better method to solve it? Which has lower time complexity.

Hi Divyam, yes we can reduce the complexity. Instead of using nested loops, we can do it in only one loop. Here is the code in which I’ve solved it using one loop, you can go through it here https://ide.codingblocks.com/s/62495