-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpairs_having_given_sum.c
53 lines (47 loc) · 1.07 KB
/
pairs_having_given_sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Date: 2018-09-23
*
* Description:
* Find pairs in array having given sum.
*
* Approach:
* Use a hashmap to keep track of what all numbers are already traversed.
*
* Complexity:
* O(n) time and space
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int num_of_elements = 0;
int *p_input = NULL;
int *p_tmp = NULL;
int required_sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num_of_elements);
p_input = (int *)malloc(sizeof(int) * num_of_elements);
p_tmp = (int *)malloc(sizeof(int) * num_of_elements);
for (i = 0; i < num_of_elements; i++) {
scanf("%d", &p_input[i]);
p_tmp[i] = 0;
}
printf("Enter required sum: ");
scanf("%d", &required_sum);
for (i = 0; i < num_of_elements; i++) {
if (p_tmp[required_sum - p_input[i]] == 1) {
printf("SUM[%d] - NUM1[%d], NUM2[%d]\n",
required_sum, p_input[i], (required_sum - p_input[i]));
}
else
{
p_tmp[p_input[i]] = 1;
}
}
return 0;
}
/* Output:
* ------------------------
* [1,2,3,4,2,3], sum = 4
* [0,1,1,0,0,0]
*/