-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion8.c
119 lines (91 loc) · 2.01 KB
/
question8.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
find a pair in an array whose sum is equal to a given number
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*METHOD1: by iterating and find suitable pairs
Time complexity: O(n^2) time
*/
int main(){
int a[] = {2,5,8,1,4,5};
int j;
printf("pairs whose sum is 6 are\n");
for(int i=0; i<6;i++){
int key1 = a[i];
for(j=i+1;j<6;j++){
int key2 = a[j];
if(key1+key2 == 6){
printf("%d and %d ",key1,key2);
printf("\n");
}
}
}
}
/*METHOD2: hash table
Insert all numbers into a hash table. O(n)
For every element a, search b in hash table such that sum is x O(1)
Time complexity: O(n) time
Space complexity: size of hash O(n) time
*/
#include <stdio.h>
#define MAX 10
void findPairs(int arr[],int size,int sum){
int index,temp;
int hash[MAX] = {0};
for(index = 0; index<size; index++){
temp = sum - arr[index];
if(temp>=0 && hash[temp] == 1){
printf("Pair with the given sum %d is %d and %d ", sum,temp,arr[index]);
}
hash[arr[index]]=1;
}
}
int main(){
int a[] = {2,5,8,1,4,5};
int length = sizeof(a)/sizeof(a[0]);
int sum = 9;
findPairs(a,length, sum);
}
/*
METHOD3: using quick sort technique
but finding if a pair exists or not
*/
#include <stdio.h>
#include <stdlib.h>
int cmpfunc(const void*a, const void*b){
return (*(int*)a-*(int*)b);
}
int findPairs(int arr[], int size, int sum){
int left, right;
qsort(arr,size,sizeof(int),cmpfunc);
left = 0;
right = size - 1;
while(left<right){
if(arr[left]+arr[right]==sum){
return 1;
}else if(arr[left]+arr[right]<sum){
left++;
}else{
right--;
}
}
}
int main(){
int size, index, sum, *arr;
printf("enter number of elements in the array\n");
scanf("%d",&size);
//allocate memory for array
arr = (int *)malloc(sizeof(int)*size);
printf("enter the elements of the array\n");
for(index=0; index<size; index++){
scanf("%d",&arr[index]);
}
printf("enter the sum value\n");
scanf("%d",&sum);
if(findPairs(arr,size,sum)){
printf("Pairs found\n");
}else{
printf("Pairs not found\n");
}
}