You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true. Else return false. For example, if the given array is {12, 3, 4, 1, 6, 9} and given sum is 24, then there is a triplet (12, 3 and 9) present in array whose sum is 24.
73
+
74
+
Recommended: Please solve it on "PRACTICE " first, before moving on to the solution.
75
+
Method 1 (Naive)
76
+
A simple method is to generate all possible triplets and compare the sum of every triplet with the given value. The following code implements this simple method using three nested loops.
77
+
CJava
78
+
# include <stdio.h>
79
+
80
+
// returns true if there is triplet with sum equal
81
+
// to 'sum' present in A[]. Also, prints the triplet
82
+
bool find3Numbers(int A[], int arr_size, int sum)
83
+
{
84
+
int l, r;
85
+
86
+
// Fix the first element as A[i]
87
+
for (int i = 0; i < arr_size-2; i++)
88
+
{
89
+
// Fix the second element as A[j]
90
+
for (int j = i+1; j < arr_size-1; j++)
91
+
{
92
+
// Now look for the third number
93
+
for (int k = j+1; k < arr_size; k++)
94
+
{
95
+
if (A[i] + A[j] + A[k] == sum)
96
+
{
97
+
printf("Triplet is %d, %d, %d", A[i], A[j], A[k]);
98
+
return true;
99
+
}
100
+
}
101
+
}
102
+
}
103
+
104
+
// If we reach here, then no triplet was found
105
+
return false;
106
+
}
107
+
108
+
/* Driver program to test above function */
109
+
int main()
110
+
{
111
+
int A[] = {1, 4, 45, 6, 10, 8};
112
+
int sum = 22;
113
+
int arr_size = sizeof(A)/sizeof(A[0]);
114
+
115
+
find3Numbers(A, arr_size, sum);
116
+
117
+
return 0;
118
+
}
119
+
Run on IDE
120
+
121
+
Output:
122
+
Triplet is 4, 10, 8
123
+
Time Complexity: O(n^3)
124
+
125
+
126
+
127
+
Method 2 (Use Sorting)
128
+
Time complexity of the method 1 is O(n^3). The complexity can be reduced to O(n^2) by sorting the array first, and then using method 1 of this post in a loop.
129
+
1) Sort the input array.
130
+
2) Fix the first element as A[i] where i is from 0 to array size – 2. After fixing the first element of triplet, find the other two elements using method 1 of this post.
131
+
C++Java
132
+
// C++ program to find a triplet
133
+
# include <bits/stdc++.h>
134
+
using namespace std;
135
+
136
+
// returns true if there is triplet with sum equal
137
+
// to 'sum' present in A[]. Also, prints the triplet
138
+
bool find3Numbers(int A[], int arr_size, int sum)
139
+
{
140
+
int l, r;
141
+
142
+
/* Sort the elements */
143
+
sort(A, A+arr_size);
144
+
145
+
/* Now fix the first element one by one and find the
146
+
other two elements */
147
+
for (int i=0; i<arr_size-2; i++)
148
+
{
149
+
150
+
// To find the other two elements, start two index
151
+
// variables from two corners of the array and move
0 commit comments