-
Notifications
You must be signed in to change notification settings - Fork 3
/
question15.c
82 lines (64 loc) · 1.55 KB
/
question15.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
/*
Find the triplet in given array that sum to given value 'x'
METHOD1:
Find all possible combinations
Time complexity: O(n^3)
Space complexity: O(1)
METHOD2:
Sorting and then fixing one number and using two pointers one at 1st position and 1 at last, and checking combinations.
If nothing found fixing the next number and trying again
Time complexity: O(nlogn) + O(n^2) which is O(n^2)
Best case:O(n^2) worst case
Worst case: O(n)
Space complexity: O(1)
*/
// METHOD1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int a[] = {4,3,6,8,2,1,0,9};
int length = sizeof(a)/sizeof(a[0]);
int i,j,k, sum;
for(i=0; i<length;i++){
int num1 = a[i];
for (j=i+1; j<length;j++){
int num2 = a[j];
for(k=j+1;k<length;k++){
sum = num1 + num2 + a[k];
if(sum == 10){
printf("elements whose sum is %d are %d, %d and %d\n", 10,num1,num2,a[k]);
}
}
}
}
}
//METHOD2
#include <stdio.h>
#include <stdlib.h>
int compar(void const*a, void const *b){
return (*(int*)a-*(int*)b);
}
int main(){
int a[] = {4,3,6,8,2,1,0,9};
int length = sizeof(a)/sizeof(a[0]);
qsort(a,length,sizeof(int),compar);
int num1, num2, num3,j,k, sum = 10;
for(int i=0; i<length-2;i++){ //as last two will be covered in while loop always for second and third num
num1 = a[i];
j = i+1;
k = length-1;
while(j<k){
num2 = a[j];
num3 = a[k];
if(num1 + num2 + num3 == sum){
printf("the numbers whose sum is %d are %d, %d and %d\n", 10,num1,num2,num3);
}
if(num1 + num2 + num3 > sum){
k--;
}else{
j++;
}
}
}
}