Skip to content

Commit b1977a2

Browse files
authored
Merge pull request ashutosh97#147 from Lakshay-agarwal/master
Added triplet_problem
2 parents 7a31989 + 58f8a6a commit b1977a2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

triplet_problem/question.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Find a triplet such that sum of two equals to third element in an array.

triplet_problem/solution.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<stdio.h>
2+
3+
void sort(int a[],int);
4+
void findtriplet(int a[],int n)
5+
{
6+
sort(a,n);
7+
for(int k=n-1;k>=0;k--)
8+
{
9+
int i=0,j=k-1;
10+
while(i<j)
11+
{
12+
if(a[k] == a[i]+a[j])
13+
{
14+
printf("%d %d %d\n",a[i],a[j],a[k]);
15+
return;
16+
}
17+
else if(a[k] > (a[i]+a[j]))
18+
i++;
19+
else
20+
j--;
21+
}
22+
}
23+
printf("No such triplet exists\n");
24+
}
25+
26+
void sort(int a[],int n)
27+
{
28+
for(int i=0;i<n-1;i++)
29+
{
30+
for(int j=0;j<=n-1-i;j++)
31+
{
32+
if(a[j] > a[j+1])
33+
{
34+
int t;
35+
t = a[j];
36+
a[j] = a[j+1];
37+
a[j+1] = t;
38+
}
39+
}
40+
}
41+
}
42+
43+
int main()
44+
{
45+
int a[9] = {5,32,1,7,10,50,15,21,2};
46+
findtriplet(a,9);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)