forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_of_triplet.c
94 lines (77 loc) · 2.3 KB
/
sum_of_triplet.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
/*Take as input N, the size of array. Take N more inputs and store that in an array. Take as input “target”, a number. Write a function which prints all triplets of numbers which sum to target.
For example
input:
9
5 7 9 1 2 4 6 8 3
10
output:
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 3 and 5
*/
#include<stdio.h>
#include<stdlib.h>
void sort(int a[],int n)
{
//using bubble sort
int i,j,temp;
for(i=0; i<n; i++)
{
for(j=0; j<=(n-2)-i; j++)
{
if(a[j] > a[j+1] ) // if a[j] > a[j+1] then swap them
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
void tripletSum(int a[],int n,int target)
{
int i=0, j,k;
for(i=0; i<n; i++ ) // to traverse the whole array we need a foor loop from i to n-1
{
j = i+1; k = n-1; // using 2 iterator approach
while(j<k) // loop will stop when j==k
{
if(a[i] + a[j] + a[k] == target) //if triplet sum is equal to target then print it
{
printf("%d, %d and %d\n",a[i],a[j],a[k]);
j++; k--; //update both iterator as we have find a triplet sum equal to target
}
else if(a[i] + a[j] + a[k] > target) // if triplet sum is less than target then decrement the 3 iterator k
{
k--;
}
else // increment the 2nd iterator j as triplet sum is greater than target
{
j++;
}
}
}
}
int main()
{
int *a, n,i,target;
scanf("%d",&n); //input no. of elements in array
a = (int*)calloc(n , sizeof(int)); // take dynamic array as they are not of fixed size and are allocated on heap
for(i=0; i<n; i++)
{
scanf("%d",&a[i]); // input elements in array
}
scanf("%d",&target); //input the target number
/* We need to sort the array as the output is in sorted form, also it would be easier to check the triplets in forward direction */
sort(a,n); // sort the array
//the solution is based on 2 pointer approach to find pair sum equal to target
tripletSum(a,n,target); //call tripletSum() to find and print triplet
free(a); //deallocate the array because dynamic array needs to be cleared after the use
return 0;
}
/* Time Complexity of sort function : O(n^2)
Time Complexity of tripletSum function : O(n^2)
Space Complexity of sort function : O(1) because only 1 extra space is required for temp
Space Complexity of tripletSum function : O(1) because no extra space is required
*/