given a list of numbers and a number k return whether any two numbers from the list add up to k.
Answers
Answered by
0
I guess you want either the algorithm or the program for this question. If so, then consider the below:
#include <stdio.h>
#define MAX 100000
void function(int a[], int length, int sum)
{
int i, temp;
bool s[MAX] = {0};
for (i = 0; i < length; i++)
{
temp = sum - a[i];
if (temp >= 0 && s[temp] == 1)
printf("Pair with given sum %d is (%d, %d) n",
sum, a[i], temp);
s[arr[i]] = 1;
}
}
int main()
{
int A[] = {7, 4, 45, 6, 48, 8};
int n = 55;
int length = sizeof(A)/sizeof(A[0]);
function(A, length, n);
getchar();
return 0;
}
Here we are sorting the array first and then applying the function. So it will be easier to get the solution.
Similar questions
History,
7 months ago
Physics,
7 months ago
Social Sciences,
1 year ago
Social Sciences,
1 year ago
Math,
1 year ago