Math, asked by neeleshkumar9955, 1 year ago

given a list of numbers and a number k return whether any two numbers from the list add up to k.

Answers

Answered by imhkp4u
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