+ 1
please solve this algorithm and give the logic.
Given a list of numbers can you find all the pairs of numbers whose sum equals k? For example, when given the list1 9 11 13 2 3 7 5 and k as 12, you should find these pairs: {1 11}, {9 3} and {7, 5}.
2 Answers
+ 5
You have a list, Let a = [1,9,11,13,2,3,7,5] and k=12.
Now,
int i,j,k,a[30],b[15][2],z=0;
for(i=0;i<=7;i++) //7 because list has 8 terms
{
for(j=i+1;j<=7;j++)//These 2 for loops cover every combination without repetition
if(a[i]+a[j]==k)//This checks the sum
{
b[z][0]=a[i]; // Now we store the two pairs in b
b[z][1]=a[j];
z++;
}}
Then print elements of b using for loop.
+ 1
thanks Lakshya