Where am i going wrong in this code?
@Nitya_Somani,
In the code that you submitted, you are not sorting the array.
Input:
5
5
2
3
4
1
6
Output:
5 and 1
2 and 4
correct output:
1 and 5
2 and 4
A better approach:
Algorithm:
1.First sort the given array.
2.Now take two variables one as left and other as right starting from 0th and end index of the sorted array respectively.
3.Now iterate till left<right.
3.1 Calculate the sum of the elements at left and right position
3.1.1 If the sum is equal to the target then print both the elements. //printing target sum pairs
3.1.2 If the sum is less than the target then increase the left by 1
3.1.3 Else decrease the right by 1
i have sorted the array by selectionsorting function();
I am not getting the correct output with the above approach please look into the code once .
@Nitya_Somani,
Mate you don’t have to display the array as well.
Also,
For the sample test case:
5
1
3
4
2
5
5
Your output (after not printing the array and adding space before and after ‘and’ ":
5 and 2
5 and 2
5 and 2
5 and 2
Correct output:
1 and 4
2 and 3
@Nitya_Somani,
You are taking sum= low+high ;
This is the sum of indexes. I have given you the approach, an additional test case as well. It’s not that hard. Try to figure it out 